File-1:
<?php
<div>
<p>
something...
</p>
</div>
?>
File-2:
<?php
$file1= What should I write to get output of File1?
?>
I need to get html output to the $file1 variable; Please help.
I think this will work for you:
function read($file) {
ob_start();
include($file);
$content = ob_get_contents();
ob_end_clean();
return $content;
}
Use it like this:
$file = 'path/to/your/file.php';
$content = read($file);
To pass variables you can modify function above like this:
function read($file, $vars) {
ob_start();
extract($vars, EXTR_SKIP);
include($file);
$content = ob_get_contents();
ob_end_clean();
return $content;
}
$file = 'path/to/your/file.php';
$vars = array(
'var1' => 'value',
);
$content = read($file, $vars);
You can access variables in included file like this: print $var1;
you can use
http://www.php.net/manual/en/function.ob-start.php
to start output buffering, then include your file and use
http://www.php.net/manual/en/function.ob-get-contents.php
to read what was the output and finish with
http://www.php.net/manual/en/function.ob-end-clean.php
to end the buffering.
Maybe is this you are looking for:
http://www.tizag.com/phpT/fileread.php
Visit the section: PHP - File Read: fread Function
Use this code:
$content = file_get_contents('somefile.txt');
You could either set the variable in File-1
and include it:
include()
/ include_once()
/ require()
/ require_once()
Or you could print the contents of File-1
and use output buffering
: