I'm trying to get the contents of a PHP file after it is parsed, and then store it in a variable. I couldn't get any useful information via Google, except for this one example:
ob_start();
include $file;
$content = ob_get_clean();
But this returns the contents as plain text, i.e.: The <?php
and ?>
tags are still there, and all code between the tags isn't parsed.
So I wanted to know, how can I do this properly?
update: This is content of the file which is being included:
Testcontent
<?php echo 'This should be parsed, right?'; ?>
Based on Tyil's last comment he wants to entirety of the php file within a variable:
include.php:
I was using this function several years ago for a sort of a template engine, it seems to do what you need - pass a string with some PHP code inside and it will return it with PHP executed. Surprisingly, it still works :-)
If you can modify your
$file
, than usereturn
in it. Otherwise cURL it (opens a web page like browser does).To do this, you have to use cURL.
Well, if you want to do it effectively, anyhow.
I know I'm several years late, but I was also looking for a solution to this issue, and I'd like to share the solution. Keep in mind, while this script does indeed get a PHP HTML page parsed, downloading via the web protocol is very slow, at least in my tests.
...don't thank me. Thank David Walsh. (https://davidwalsh.name/curl-download)
This should definitely work. And it does for me:
You might want to look at your included file (named in
$file
) and see if there is perhaps some strange character after the initial<?php
that might cause it not to be interpreted as a PHP script.To see a hex dump of what's in the file (so you can see what character actually follows
<?php
rather than what's displayed in your editor), use:od -c filename.php | less
.