How do you define a PHP include as a string?

前端 未结 6 1356
我在风中等你
我在风中等你 2021-01-01 12:48

I tried:

$test = include \'test.php\';

But that just included the file normally

相关标签:
6条回答
  • 2021-01-01 13:16

    You'll want to look at the output buffering functions.

    //get anything that's in the output buffer, and empty the buffer
    $oldContent = ob_get_clean();
    
    //start buffering again
    ob_start();
    
    //include file, capturing output into the output buffer
    include "test.php";
    
    //get current output buffer (output from test.php)
    $myContent = ob_get_clean();
    
    //start output buffering again.
    ob_start();
    
    //put the old contents of the output buffer back
    echo $oldContent;
    

    EDIT:

    As Jeremy points out, output buffers stack. So you could theoretically just do something like:

    <?PHP
    function return_output($file){
        ob_start();
        include $file;
        return ob_get_clean();
    }
    $content = return_output('some/file.php');
    

    This should be equivalent to my more verbose original solution.

    But I haven't bothered to test this one.

    0 讨论(0)
  • 2021-01-01 13:21

    The other answers, for reasons unknown to me, don't quite reach the correct solution.

    I suggest using the buffer, but you have to get the contents and then clean the buffer before the end of the page, otherwise it is outputted. Should you wish to use the output from the included file, you should use op_get_contents(), which will return a string of the contents of the buffer.

    You also don't need to loop over the includes as each will just add to the buffer (unless you clean it first).

    You therefore could use the following;

    ob_start();
    include_once('test.php');
    include_once('test2.php');
    $contents = ob_get_contents();
    ob_end_clean();
    

    Hope this helps.

    0 讨论(0)
  • 2021-01-01 13:29

    Try file_get_contents().

    This function is similar to file(), except that file_get_contents() returns the file in a string.

    0 讨论(0)
  • 2021-01-01 13:30

    You can use the function file_get_contents.

    0 讨论(0)
  • 2021-01-01 13:31

    Try something like:

    ob_start();
    include('test.php');
    $content = ob_get_clean();
    
    0 讨论(0)
  • 2021-01-01 13:38

    Solution #1: Make use of include (works like a function): [My best solution]

    File index.php:

    <?php
    $bar = 'BAR';
    $php_file = include 'included.php';
    print $php_file;
    ?>
    

    File included.php:

    <?php
    $foo = 'FOO';
    return $foo.' '.$bar;
    ?>
    <p>test HTML</p>
    

    This will output FOO BAR, but Note: Works like a function, so RETURN passes contents back to variable (<p>test HTML</p> will be lost in the above)


    Solution #2: op_buffer():

    File index.php:

    <?php
    $bar = 'BAR';
    ob_start();
    include 'included.php';
    
    $test_file = ob_get_clean(); //note on ob_get_contents below
    print $test_file;
    ?>
    

    File included.php:

    <?php
    $foo = 'FOO';
    print $foo.' '.$bar;
    ?>
    <p>test HTML</p>
    

    If you use ob_get_contents() it will output FOO BAR<p>test HTML</p> TWICE, make sure you use ob_get_clean()


    Solution #3: file_get_contents():

    File index.php:

    <?php
    $bar = 'BAR';
    $test_file = eval(file_get_contents('included.php'));
    
    print $test_file;
    ?>
    

    File included.php:

    $foo = 'FOO';
    print $foo.' '.$bar;
    

    This will output FOO BAR, but Note: Include.php should not have <?php opening and closing tags as you are running it through eval()

    0 讨论(0)
提交回复
热议问题