0

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?'; ?>
hakre
  • 193,403
  • 52
  • 435
  • 836
Tyil
  • 1,797
  • 9
  • 14
  • 2
    What you show should work - the code should get executed in the context of the current script. Can you show an example of what the included file contains? – Pekka Feb 27 '12 at 14:46
  • And when you say you want to "get the contents of a PHP file", what exactly do you mean? `include()` will *execute* a PHP file, but are you asking about reading it as if it were a text file? More details on what you're actually trying to achieve would let us help you better. You want help to find a solution to your problem, not help to make a pre-determined solution work regardless of whether it is appropriate. – ghoti Feb 27 '12 at 14:49
  • Well, exactly as I say it: I want a PHP file to be parsed, and the contents to be returned, so I can put it in a variable. – Tyil Feb 27 '12 at 14:51
  • Instead, you could try `$var = include 'file.php';` and in file.php do `return 'data';` (haven't tried this, but I think it should work). – Bart S. Feb 27 '12 at 14:53
  • Okay, but "get the contents" does not mean "parsed". :-) – ghoti Feb 27 '12 at 14:54
  • Well, I did mention "after it is parsed" in the question :/ – Tyil Feb 27 '12 at 14:58
  • Tyil, what if my include file looks like this: ` – Mike B Feb 27 '12 at 15:01
  • 1
    @MikeB `$content` contains the string ` – Tyil Feb 27 '12 at 15:07
  • I wonder what can be a practical purpose of this – itachi Feb 27 '12 at 15:14

5 Answers5

5

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;
}
Algis
  • 51
  • 1
  • 5
  • this response helped me answer another question [here](http://stackoverflow.com/questions/16454234/php-import-2-class-with-same-name/16462550#16462550) +1 for that. – bkdude May 09 '13 at 13:20
  • Raises an exception if you have the last statement (or the only one) ending without a semi-colon. Example: `` or ``. Both of these work fine if executed as regular PHP code. Think somebody can fix? I made my own makeshift fix for this to just add a semi colon if it doesn't exist in the end, but would love a better fix. :) – Abhishek Saini Nov 07 '16 at 17:55
1

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

MFix
  • 229
  • 1
  • 5
1

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'; ?>
Community
  • 1
  • 1
Mike B
  • 31,886
  • 13
  • 87
  • 111
  • That is indeed what I want. I hoped the original question would be sufficient to make that clear. But after setting the content of the included file to `` it still remains that string after including it. I'm not sure anymore wether this is a fault of mine, or my webhost at this point :/ – Tyil Feb 27 '12 at 15:22
0

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)

Joshua Sandoval
  • 441
  • 1
  • 6
  • 13
0

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.

ghoti
  • 45,319
  • 8
  • 65
  • 104