For example, in foo.php:
<?php echo 'hello'; ?>
And in bar.php, I want to get the output of foo.php (which is hello) and do some formatting before outputting to browser. Is there any way to do this?
Or even further, if the webserver can run both PHP and Python scripts, can a PHP script get the output of a Python script?
Edit:
PHP function file_get_contents() can do this only for remote scripts. If it is used on local scripts, it will return the contents of the whole script. In the example above, it returns rather than hello. And I don't want to use exec()/system() things and CGI.
You can use PHP's output buffers and functions like ob_start(); and ob_get_contents(); to read the contents of your PHP output into a string.
Here is the example on php.net:
<?php
function callback($buffer)
{
// replace all the apples with oranges
return (str_replace("apples", "oranges", $buffer));
}
ob_start("callback");
?>
<html>
<body>
<p>It's like comparing apples to oranges.</p>
</body>
</html>
<?php
ob_end_flush();
?>
And another:
<?php
ob_start();
echo "Hello ";
$out1 = ob_get_contents();
echo "World";
$out2 = ob_get_contents();
ob_end_clean();
var_dump($out1, $out2);
?>
Related
I am trying to reach the contents of a PHP file without the file actually outputting what it would usually do. Here is my test code:
File1 (test1.php)
<?php
ob_start();
include_once './test2.php';
$test = ob_get_contents();
echo $test;
?>
and here is file2 (test2.php)
<?php
$testVar = 'Name!';
?>
<div class="testClass"><?php echo $testVar?></div>
<p>Spam2</p>
and I want it to only do this because of the
echo $test
line NOT because the file is outputting the content.
<p>Spam2</p><div class="testClass">Name!</div>
<p>Spam2</p></body>
due to the echo, but it returns this
<p>Spam2</p><div class="testClass">Name!</div>
<p>Spam2</p></body>
<p>Spam2</p><div class="testClass">Name!</div>
<p>Spam2</p></body>
So how do I get it to only return the content once?
Don't echo $test;. PHP is executing as it should. Since ./test2.php shows Spam in HTML it appears on the page, then you assign the page contents to a variable and echo it. What do you expect?
If you have 2 files say: app/index.php and app/config.php you can just use the return keyword to return some content from the config.php file. And then, when you include the file whatever you returned from config.php can be saved to a variable.
Example:
First return whatever you want from the config.php file (could be an array, string, etc).
<?php
return ['name' => 'Spam'];
Then in the index.php:
<?php
$contents = include_once('config.php');
echo $contents;
How can I get expected output from example below?
Note: I'm using $content = file_get_contents('content.php'); to use content where and when possible so it is not a direct output on screen. include() breaks the pages.
content.php
<p>Hello <?php echo 'World!'; ?></p>
reader.php
<b>Message from another file:</b> <?php echo file_get_contents('content.php'); ?>
Output of code above is:
Message from another file: Hello <?php echo 'World!'; ?>
Instead of (expected):
Message from another file: Hello World!
I think you are looking for <?php include('content.php');
file_get_contents — Reads entire file into a string
PHP.net file_get_contents - manual
The include statement includes and evaluates the specified file.
PHP.net include - manual
Try making content.php into a file that has a function that returns the content you want (you may want to have parameters). Simply require the file then call the function and save the output.
Example:
content.php
function get_content($world){
return '<p>Hello ' . $world . '</p>';
}
reader.php
<?php
require('content.php');
$content = get_content('world');
?>
<b>Message from another file:</b> <?php echo $content; ?>
Since you cannot use include (though I don't understand fully why), but want the file to be parsed and executed as PHP code, you can use eval
<b>Message from another file:</b> <?php eval(file_get_contents('content.php')); ?>
But the file content.php should not contain <?php and ?> tags, as stated at http://php.net/eval.
So say I have a file page.php
<html>
Hello <?php echo $world;?>
</html>
I want to create a variable and assign it 'Hello world' using page.html
Something like
$world = 'world';
$mypage = parse_file('page.php');
I suppose I could wrap the entire file in doublequotes as such
$mypage="<html>
Hello $world
</html>";
but doublequote parsing is inefficient. Is there any easy of doing this without having to write a script that parses page.html?
Output buffering will do this nicely
ob_start();
$world = 'world';
include('page.php');
$mypage = ob_get_clean();
Reference:
ob_start()
ob_get_clean()
An alternative is using heredoc syntax
I want to be able to put PHP into the database and run it. I have to do this because I store page layouts in the database and each our different for each other, however in some cases I want to use dynamic content for some of the pages.
Assume $query_from_db is the string returned from the database. PHP should only eval() the code in between <?php and ?>
$query_from_db = '<div>
<?php
//php to run
function dosomething() {
//bleh
}
?>
</div>
';
php echo eval($query_from_db);
How can I do this? I'm aware this is not recommended.
I'm not arguing about the sense or nonsense of this approach. To some extend, this is a valid question.
See the documentation:
To mix HTML output and PHP code you can use a closing PHP tag to leave PHP mode.
So you have to do:
eval('?> ' . $query_from_db . ' <?php ');
DEMO
Also note that eval is outputting directly to the browser. It does not return a value. Have a look at Output Control Functions for buffering.
You are aware that this is not recommended and I strongly urge everyone to review the comments to this question.
But to provide an answer:
<?php
$string = 'hello <?php echo "world"; ?>';
eval('?>'.$string.'<?'); // will output "hello world";
be aware that this however will not work:
<?php
$string = 'hello <?php echo "world"; ?>';
eval('?>'.$string.'<?php'); // error will be thown
This works again:
<?php
$string = 'hello <?php echo "world"; ?>';
eval('?> '.$string.' <?php '); // will output "hello world";
i am not really sure why.
following up on your comment to grab the output you can do:
<?php
$string = 'hello <?php echo "world"; ?>';
ob_start();
eval('?> '.$string.' <?php '); // will output "hello world";
$output = ob_get_clean(); // $output will now contain "hello world". No text will have ben printed.
If you want to avoid the eval stigmata, you can alternatively use:
include("data:,$query_from_db");
It's just another name for eval which doesn't upset people as much. It depends on the php.ini setting allow_url_include however.
What you are doing is functionally equivalent to include("$template/$by_name.php"); and just differs in that you didn't put the database content into a file before. (But that's the other workaround: file_put_contents && include).
Okay, so maybr I'm going about doing this entirely wrong, I probably am. But I would like to be able to take the HTML between a ... like so:
$str = ?>
... some HTML goes here ...
<?php ;
Am I completely off my rocker to think I can do this? I couldn't think of a way to put it into words so I could search it on Google, which is why I'm here...
You can use output buffering:
ob_start();
?>
... some HTML goes here ...
<?php
echo 'php outputs are captured too';
$str = ob_get_contents();
ob_end_clean();
Alternatively, if it's just a little bit of HTML (and no php code within), just write it down with one of the string formats like heredoc or nowdoc:
$str = <<<'NOWDOC'
... some HTML goes here
NOWDOC;
Look into heredocs and nowdocs. A heredoc looks like:
$str = <<<HTML
<div>This is some text!</div>
HTML;
// We're back in PHP.
echo $str;
If you specifically want to work with HTML, look into XHP.
Just wanted to add to phihag's answer.
It is possible to capture HTML with a function as well, including with anonymous functions:
<?php $bob = function() { ?>
... some HTML here...
<?php }; ?>
and later output $bob:
<?php $bob(); ?>
or capture the output of $bob somewhere else with output buffering:
ob_start();
$bob();
$str = ob_get_contents();
ob_end_clean();
PHP has a multiline, specially delimited string for such situations.
This talks about it a little.