如何将PHP包含定义为字符串?

时间:2020-12-14 02:07:09

I tried:

我试过了:

$test = include 'test.php';

But that just included the file normally

但那只是正常包含文件

6 个解决方案

#1


29  

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:

正如Jeremy指出的那样,输出缓冲区堆栈。所以理论上你可以做以下事情:

<?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.

但我没有费心去测试这个。

#2


10  

Try something like:

尝试以下方法:

ob_start();
include('test.php');
$content = ob_get_clean();

#3


8  

Try file_get_contents().

尝试file_get_contents()。

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

此函数类似于file(),但file_get_contents()以字符串形式返回文件。

#4


5  

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

解决方案#1:使用include(像函数一样工作):[我的最佳解决方案]

File index.php:

文件index.php:

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

File included.php:

文件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)

这将输出FOO BAR,但注意:像函数一样工作,所以RETURN将内容传递回变量(

测试HTML 将在上面丢失)


Solution #2: op_buffer():

解决方案#2:op_buffer():

File index.php:

文件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:

文件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()

如果你使用ob_get_contents(),它将输出FOO BAR

测试HTML TWICE,确保你使用ob_get_clean()


Solution #3: file_get_contents():

解决方案#3:file_get_contents():

File index.php:

文件index.php:

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

print $test_file;
?>

File included.php:

文件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()

这将输出FOO BAR,但注意:当你通过eval()运行时,Include.php不应该有<?php开启和关闭标签

#5


0  

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.

我建议使用缓冲区,但你必须获取内容,然后在页面结束前清理缓冲区,否则输出。如果您希望使用包含文件的输出,则应使用op_get_contents(),它将返回缓冲区内容的字符串。

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.

希望这可以帮助。

#6


-1  

You can use the function file_get_contents.

您可以使用函数file_get_contents。

#1


29  

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:

正如Jeremy指出的那样,输出缓冲区堆栈。所以理论上你可以做以下事情:

<?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.

但我没有费心去测试这个。

#2


10  

Try something like:

尝试以下方法:

ob_start();
include('test.php');
$content = ob_get_clean();

#3


8  

Try file_get_contents().

尝试file_get_contents()。

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

此函数类似于file(),但file_get_contents()以字符串形式返回文件。

#4


5  

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

解决方案#1:使用include(像函数一样工作):[我的最佳解决方案]

File index.php:

文件index.php:

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

File included.php:

文件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)

这将输出FOO BAR,但注意:像函数一样工作,所以RETURN将内容传递回变量(

测试HTML 将在上面丢失)


Solution #2: op_buffer():

解决方案#2:op_buffer():

File index.php:

文件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:

文件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()

如果你使用ob_get_contents(),它将输出FOO BAR

测试HTML TWICE,确保你使用ob_get_clean()


Solution #3: file_get_contents():

解决方案#3:file_get_contents():

File index.php:

文件index.php:

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

print $test_file;
?>

File included.php:

文件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()

这将输出FOO BAR,但注意:当你通过eval()运行时,Include.php不应该有<?php开启和关闭标签

#5


0  

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.

我建议使用缓冲区,但你必须获取内容,然后在页面结束前清理缓冲区,否则输出。如果您希望使用包含文件的输出,则应使用op_get_contents(),它将返回缓冲区内容的字符串。

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.

希望这可以帮助。

#6


-1  

You can use the function file_get_contents.

您可以使用函数file_get_contents。