Get the parsed PHP file

I am trying to get the contents of a PHP file after parsing it, and then save it in a variable. I could not get any useful information via Google except this example:

ob_start(); include $file; $content = ob_get_clean(); 

But this returns the contents in plain text, i.e. <?php and ?> tags still exist, and all the code between the tags is not parsed.

So, I wanted to know how I can do it right?


update: This is the contents of the file that is included in the package:

 Testcontent <?php echo 'This should be parsed, right?'; ?> 
0
source share
6 answers

Based on Thiel's last comment, he wants the whole php file to be inside the variable:

Tyil, what if my include file looks like this: <?php echo 'test stuff'; $foo = 'one'; <?php echo 'test stuff'; $foo = 'one'; . What does $ content contain and what happens if I try to access $ foo from the included file?

The contents of @MikeB $ contain the string <?php echo 'test stuff'; $foo = 'one';. var_dump($foo); <?php echo 'test stuff'; $foo = 'one';. var_dump($foo); in the included file returns NULL.

 <?php $file = 'include.php'; $content = file_get_contents($file); var_dump($content); // (string) "<?php echo 'This should be parsed, right?'; $foo = 'one'; ?>" 

include.php:

 <?php echo 'This should be parsed, right?'; $foo = 'one'; ?> 
+1
source

I used this function several years ago for a kind of template engine, it seems to do what you need - pass a string with some PHP code inside and it will return it using PHP. Surprisingly, it still works :-)

 function process_php( $str ) { $php_start = 0; $tag = '<?php'; $endtag = '?>'; while(is_long($php_start = strpos($str, $tag, $php_start))) { $start_pos = $php_start + strlen($tag); $end_pos = strpos($str, $endtag, $start_pos); //the 3rd param is to start searching from the starting tag - not to mix the ending tag of the 1st block if we want for the 2nd if (!$end_pos) { echo "template: php code has no ending tag!", exit; } $php_end = $end_pos + strlen($endtag); $php_code = substr($str, $start_pos, $end_pos - $start_pos); if( strtolower(substr($php_code, 0, 3)) == 'php' ) $php_code = substr($php_code, 3); // before php code $part1 = substr($str, 0, $php_start); // here set the php output ob_start(); eval($php_code); $output = ob_get_contents(); ob_end_clean(); // after php code $part2 = substr($str, $php_end, strlen($str)); $str = $part1 . $output . $part2; } return $str; } 
+5
source

If you can change your $file , than use return in it. Otherwise, cURL it (opens a web page, such as a browser).

+1
source

This should definitely work. And this is for me:

 [ ghoti@pc ~/tmp]$ cat file1.php #!/usr/local/bin/php <?php $file="file2.php"; include($file); [ ghoti@pc ~/tmp]$ cat file2.php <?php echo "This should be parsed, right?\n"; ?> [ ghoti@pc ~/tmp]$ ./file1.php This should be parsed, right? [ ghoti@pc ~/tmp]$ 

You might want to look at your file (named in $file ) and see if there is any strange character after the initial <?php that might not interpret it as a PHP script.

To see the hexadecimal dump of what is in the file (so you can see which character actually follows <?php , and not what is displayed in your editor), use: od -c filename.php | less od -c filename.php | less .

0
source

For this you need to use cURL.

Well, if you want to do it effectively, anyway.

I know that I am several years late, but I was also looking for a solution to this problem, and I would like to share this solution. Keep in mind, while this script does process the PHP PHP page, loading via the web protocol is very slow, at least in my tests.

 function GetHTMLPage($url) { $ch = curl_init(); $timeout = 5; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $data = curl_exec($ch); curl_close($ch); return $data; } 

... do not thank me. Thanks to David Walsh. ( https://davidwalsh.name/curl-download )

0
source

Just .. You need to add the full path to the file if you need to call it through the browser.

 $content = file_get_contents("http://localhost/project/file.php"); 
-1
source

Source: https://habr.com/ru/post/1479851/


All Articles