gpt4 book ai didi

PHP - 有没有办法将 "include"字符串作为文件?

转载 作者:行者123 更新时间:2023-12-03 20:36:46 27 4
gpt4 key购买 nike

有一种已知的方法可以在加载时包含文件并将其内容捕获到字符串中。
$string = get_include_contents('somefile.php');function get_include_contents($filename) {if (is_file($filename)) {ob_start();include $filename;return ob_get_clean();}return false;}
http://php.net/manual/en/function.include.php

有没有办法“包含”从字符串而不是文件加载内容的方法?

我的意思是这样的:
$string = file_get_contents("file.php");include_from_string($string);

最佳答案

如果你想把字符串解析为PHP代码,就像用include()加载的文件内容一样,那么你需要的函数是 eval() .

请注意,与 include() 加载的代码不同, 由 eval() 执行的代码自动以 PHP 模式启动,因此您不需要(也不应该!)添加前缀 <?php .如果你想模拟 include() 的行为确切地说,您可以将字符串前缀为 eval()编辑 ?>退出 PHP 模式:

$string = file_get_contents( 'somefile.php' );
eval( '?>' . $string );

另请注意 eval()是一个非常危险的功能!虽然在这种特定情况下,它不应该比 include() 风险更大。本身就是,使用 eval()在任何可能包含未净化(或未充分净化)的用户输入的字符串上是 极度危险 ,并且可能被攻击者利用在您的系统上执行恶意代码,从而获得对其的控制。

关于PHP - 有没有办法将 "include"字符串作为文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21410706/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com