I tried:
$test = include 'test.php';
But that just included the file normally
You'll want to look at the output buffering functions.
//get anything that's in the output buffer, and empty the buffer
$oldContent = ob_get_clean();
//start buffering again
ob_start();
//include file, capturing output into the output buffer
include "test.php";
//get current output buffer (output from test.php)
$myContent = ob_get_clean();
//start output buffering again.
ob_start();
//put the old contents of the output buffer back
echo $oldContent;
EDIT:
As Jeremy points out, output buffers stack. So you could theoretically just do something like:
<?PHP
function return_output($file){
ob_start();
include $file;
return ob_get_clean();
}
$content = return_output('some/file.php');
This should be equivalent to my more verbose original solution.
But I haven't bothered to test this one.
Try something like:
ob_start();
include('test.php');
$content = ob_get_clean();
Try file_get_contents().
This function is similar to file(), except that file_get_contents() returns the file in a string.
Solution #1: Make use of include (works like a function): [My best solution]
File index.php:
<?php
$bar = 'BAR';
$php_file = include 'included.php';
print $php_file;
?>
File included.php:
<?php
$foo = 'FOO';
return $foo.' '.$bar;
?>
<p>test HTML</p>
This will output FOO BAR, but
Note: Works like a function, so RETURN passes contents back to variable (<p>test HTML</p> will be lost in the above)
Solution #2: op_buffer():
File index.php:
<?php
$bar = 'BAR';
ob_start();
include 'included.php';
$test_file = ob_get_clean(); //note on ob_get_contents below
print $test_file;
?>
File included.php:
<?php
$foo = 'FOO';
print $foo.' '.$bar;
?>
<p>test HTML</p>
If you use ob_get_contents() it will output FOO BAR<p>test HTML</p> TWICE, make sure you use ob_get_clean()
Solution #3: file_get_contents():
File index.php:
<?php
$bar = 'BAR';
$test_file = eval(file_get_contents('included.php'));
print $test_file;
?>
File included.php:
$foo = 'FOO';
print $foo.' '.$bar;
This will output FOO BAR, but Note: Include.php should not have <?php opening and closing tags as you are running it through eval()
The other answers, for reasons unknown to me, don't quite reach the correct solution.
I suggest using the buffer, but you have to get the contents and then clean the buffer before the end of the page, otherwise it is outputted. Should you wish to use the output from the included file, you should use op_get_contents(), which will return a string of the contents of the buffer.
You also don't need to loop over the includes as each will just add to the buffer (unless you clean it first).
You therefore could use the following;
ob_start();
include_once('test.php');
include_once('test2.php');
$contents = ob_get_contents();
ob_end_clean();
Hope this helps.
You can use the function file_get_contents.
Related
I want to include a file, but instead of printing output I want to get it as string.
For example, I want include a file:
<?php echo "Hello"; ?> world!
But instead of printing Hello world! while including the file I want to get it as a string.
I want to filter some elements from the file, but not from whole php file, but just from the html output.
Is it possible to do something like this?
You can use php buffers like this:
<?php
ob_start();
include('other.php');
$script = ob_get_contents(); // it will hold the output of other.php
ob_end_clean();
EDIT: You can abstract this into a function:
function inlcude2string($file) {
ob_start();
include($file);
$output = ob_get_contents(); // it will hold the output of other.php
ob_end_clean();
return $output;
}
$str = inlcude2string('other.php');
I would like to load different .php page (the .php contain html and some php variable need to be replaced.
For example:
load.php
$output = '';
load the test.php and replace that $this->name with value.
store the html to $output
load the test1.php and replace that $this->name with value.
append to the previous $output variable
so at the end i would have a $output variable have all the updated html
Any suggestion is appreciated.
test.php
>
<html>
<?php echo $this->name; ?>
</html>
test1.php
>
<html>
<?php echo $this->address; ?>
</html>
You likely want to use output buffering with a require or include statement:
ob_start();
require('load.php');
$output = ob_get_contents();
ob_end_clean();
$output should contain the contents of load.php with any variables processed.
To process multiple files (or anything else) just run it all between ob_start() and the last two lines, so you could grab two files like so:
ob_start();
require('test.php');
require('test1.php');
$output = ob_get_contents();
ob_end_clean();
I have a file B590.php which is having a lot of html code and some php code (eg logged in username, details of user).
I tried using $html = file_get_content("B590.php");
But then $html will have the content of B90.php as plain text(with php code).
Is there any method where I can get the content of the file after it has been evaluated?
There seems to be many related questions like this one and this one but none seems to have any definite answer.
You can use include() to execute the PHP file and output buffering to capture its output:
ob_start();
include('B590.php');
$content = ob_get_clean();
function get_include_contents($filename){
if(is_file($filename)){
ob_start();
include $filename;
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
return false;
}
$html = get_include_contents("/playbooks/html_pdf/B580.php");
This answer was originally posted on Stackoverflow
If you use include or require the file contents will behave as though the current executing file contained the code of that B590.php file, too. If what you want is the "result" (ie output) of that file, you could do this:
ob_start();
include('B590.php');
$html = ob_get_clean();
Example:
B590.php
<div><?php echo 'Foobar'; ?></div>
current.php
$stuff = 'do stuff here';
echo $stuff;
include('B590.php');
will output:
do stuff here
<div>Foobar</div>
Whereas, if current.php looks like this:
$stuff = 'do stuff here';
echo $stuff;
ob_start();
include('B590.php');
$html = ob_get_clean();
echo 'Some more';
echo $html;
The output will be:
do stuff here
Some more
<div>Foobar</div>
To store evaluated result into some variable, try this:
ob_start();
include("B590.php");
$html = ob_get_clean();
$filename = 'B590.php';
$content = '';
if (php_check_syntax($filename)) {
ob_start();
include($filename);
$content = ob_get_clean();
ob_end_clean();
}
echo $content;
I want to call require_once("test.php") but not display result and save it into variable like this:
$test = require_once('test.php');
//some operations like $test = preg_replace(…);
echo $test;
Solution:
test.php
<?php
$var = '/img/hello.jpg';
$res = <<<test
<style type="text/css">
body{background:url($var)#fff !important;}
</style>
test;
return $res;
?>
main.php
<?php
$test = require_once('test.php');
echo $test;
?>
Is it possible?
Yes, but you need to do an explicit return in the required file:
//test.php
<? $result = "Hello, world!";
return $result;
?>
//index.php
$test = require_once('test.php'); // Will contain "Hello, world!"
This is rarely useful - check Konrad's output buffer based answer, or adam's file_get_contents one - they are probably better suited to what you want.
“The result” presumably is a string output?
In that case you can use ob_start to buffer said output:
ob_start();
require_once('test.php');
$test = ob_get_contents();
EDIT From the edited question it looks rather like you want to have a function inside the included file. In any case, this would probably be the (much!) cleaner solution:
<?php // test.php:
function some_function() {
// Do something.
return 'some result';
}
?>
<?php // Main file:
require_once('test.php');
$result = test_function(); // Calls the function defined in test.php.
…
?>
file_get_contents will get the content of the file. If it's on the same server and referenced by path (rather than url), this will get the content of test.php. If it's remote or referenced by url, it will get the output of the script.
How can I put the result of an include into a PHP variable?
I tried file_get_contents but it gave me the actual PHP code, whereas I want whats echoed.
Either capture anything that's printed in the include file through output buffering
ob_start();
include 'yourFile.php';
$out = ob_get_contents();
ob_end_clean();
or alternatively, set a return value in the script, e.g.
// included script
return 'foo';
// somewhere else
$foo = include 'yourFile.php';
See Example 5 of http://de2.php.net/manual/en/function.include.php
or simply return a value from an included file as explained here.
return.php:
<?php
$var = 'PHP';
return $var;
?>
$foo = include 'return.php';
echo $foo; // prints 'PHP'