Get parsed PHP file

2020-05-01 07:56发布

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?'; ?>

标签: php
5条回答
beautiful°
2楼-- · 2020-05-01 08:04

Based on Tyil's last comment he wants to entirety of the php file within a variable:

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

@MikeB $content contains the string <?php echo 'test stuff'; $foo = 'one';. var_dump($foo); in the including 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'; ?>
查看更多
【Aperson】
3楼-- · 2020-05-01 08:06

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 :-)

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;
}
查看更多
迷人小祖宗
4楼-- · 2020-05-01 08:16

If you can modify your $file, than use return in it. Otherwise cURL it (opens a web page like browser does).

查看更多
Evening l夕情丶
5楼-- · 2020-05-01 08:20

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.

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;
}

...don't thank me. Thank David Walsh. (https://davidwalsh.name/curl-download)

查看更多
做自己的国王
6楼-- · 2020-05-01 08:28

This should definitely work. And it does 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 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.

查看更多
登录 后发表回答