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
Related
I'm looking for something much like the Using PHP variables inside HTML tags? question, but a little different.
In my case, I'd like to use code ore like this:
$somevar = 'a test';
include("file.html");
and file.html would contain
<b>hello, this is {$somevar}</b>
The problem is that it just prints hello, this is {$somevar}.
How can I make the HTML read the vars in the included file?
echo "<b>hello, this is {$somevar}</b>";
or
<b>hello, this is <?=$somevar?></b>
or
<b>hello, this is <?php echo $somevar; ?></b>
or
<b>hello, this is <?php print $somevar; ?></b>
You need to include the variable defining program, in the other program wanting to access it.
Example:
Say test.html has $somevar.
in file.html you do,
<?php
include('test.html');
echo "<b>hello, this is $somevar</b>";
?>
<?php
include "stuff.php";
$somevar = "test";
?>
<html>
<body><p><?php echo($somevar); ?></p></body>
</html>
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.
I need to have include page inside the php heredoc variable but it doesn't work please help me.
$content = <<<EOF
include 'links.php';
EOF;
You can do this way:
ob_start();
include 'links.php';
$include = ob_get_contents();
ob_end_clean();
$content = <<<EOF
{$include}
EOF;
Simple: you cannot do it. You can include the file before hand, store it in a variable, then insert it into the file. For example:
$links_contents = file_get_contents('links.php');
//$links_contents = eval($links_contents); // if you need to execute PHP inside of the file
$content = <<<EOF
{$links_contents}
EOF;
What do you mean by not working? As in contents of 'links.php' isn't in $content? If thats what you want try using output stream redirection (or just read the file).
<?php
ob_start();
include 'links.php';
$content = ob_get_contents();
ob_end_clean();
echo "contents=[$content]\n";
?>
Heredoc syntax is made to handle text only. You can't include a file or execute php methods in it.
Resources :
php.net - heredoc
Do not use heredoc at all.
if you need to output your contents - just output it as is, without storing it in the variable.
There can be very limited use of output buffering and I am sure here is not such case.
Just prepare your data and then output it using plain HTML and PHP.
make your pages like this (right from the other recent answer):
news.php:
<?
include "config.php"; //connect to database HERE.
$data = getdbdata("SELECT * FROM news where id = %d",$_GET['id']);
$page_title = $data['title'];
$body = nl2br($data['body']);
$tpl_file = "tpl.news.php";
include "template.php";
?>
template.php:
<html>
<head>
<title><?=$page_title?></title>
</head>
<body>
<? include $tpl_file?>
</body>
tpl.news.php
<h1><?=$page_title?></h1>
<?=$body?>
<? include "links.php" /*include your links anywhere you wish*/?>
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);
?>