为什么 PHP preg_replace {include 'date.php'} 不返回 php 并打印

发布于 2024-11-19 22:02:47 字数 522 浏览 0 评论 0 原文

为什么 PHP preg_replace {include 'date.php'} 不返回 php?没有eval如何解决?

header('Content-Type:text/plain');

$str = "Today is {include 'date.php'}.";

echo preg_replace("/\{include '(.*)\'}/e", 'file_get_contents("$1")', $str);

date.php 内容:

<?php echo date('jS \of F'); ?>, 2011


结果: 今天是 ,2011 年。

预期结果:今天是 7 月 13 日。

Why PHP preg_replace {include 'date.php'} does not return php? How to solve it without eval?

header('Content-Type:text/plain');

$str = "Today is {include 'date.php'}.";

echo preg_replace("/\{include '(.*)\'}/e", 'file_get_contents("$1")', $str);

date.php content:

<?php echo date('jS \of F'); ?>, 2011

Result: Today is <?php echo date('jS \of F'); ?>, 2011.

Expected result: Today is 13th of July.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

昵称有卵用 2024-11-26 22:02:48

通过您表达问题的方式,您似乎知道您将文件的内容包含到字符串文字中,因此它没有被评估。

您还知道 eval 不是最好使用的函数,并且想知道如何避免它。那挺好的。

您要做的就是在 date.php 中放置一个函数。将函数包含在主文件中,而不是字符串中。在您需要内容时,调用该函数。

By the way you phrased your question you seem to know that you included the contents of the file into a string literal, so it did not get evaluated.

You also know eval is not the best function to use, and want to know how to avoid it. That's good.

The thing you want to do is to put a function in date.php. Include the function, not in a string, in your main file. At the point you want the content, call the function.

泪之魂 2024-11-26 22:02:48

除非您使用eval()(我不推荐),否则您真的不能指望.php 文件会在调用时执行include() 的作用是使您所包含的文件中的代码可供脚本使用,它不仅仅是执行它。这就是您的网络服务器调用的引擎所做的事情。如果你想这样做,你必须有“创意”,比如 date.php 的网址上的“nofollow">file_get_contents(),这将使 Web 服务器执行它并将结果返回给您。

实际上,在这种情况下您想要做什么,如果维护 date.php 中的代码确实很重要,请编写一个返回日期字符串的函数,然后在需要时调用它。您甚至可以将其包装在 date.php 中的类中(只是不要将其称为“Date”),然后使用 MyDate::myDateFunction() 调用它,其中您可以想要执行它。

Unless you use eval() (which I do not recommend), you really can't just expect that .php files will get executed on call. What include() does is make the code in the file you included available to your script, it doesn't just execute it. That's what the engine your web server calls does. If you wanted to do that, you'd have to be "creative" with something like file_get_contents() on the web address of date.php, which would make the web server execute it and return the result to you.

Really, what you want to do in this case, if it's really that important to maintain distinction for the code in date.php, write a function that returns the date string and then call it whenever you want to. You can even wrap it in a class in date.php (just don't call it "Date") and then call it with MyDate::myDateFunction() where you want to execute it.

玻璃人 2024-11-26 22:02:48

更改 date.php 以返回值:

<?php return date('jS \of F') . ', 2011';

然后,在主文件中,您可以执行以下操作:

$date = include 'date.php';
echo 'Today is ' . $date;

它可以工作,但不是很好。我建议重构您的代码。

Change date.php to return the value instead:

<?php return date('jS \of F') . ', 2011';

Then, in your main file, you can do:

$date = include 'date.php';
echo 'Today is ' . $date;

It works, but it's not very nice. I would recommend refactoring your code.

烟花易冷人易散 2024-11-26 22:02:48

如果你想捕获(不仅仅是打印)包含的 PHP 脚本的输出,而你无法改变它,你必须做一个丑陋的解决方法:

<?php

ob_start();
include 'date.php';
$date = ob_get_contents();
ob_end_clean();

$str = "Today is {$date}.";

?>

If you want to capture (not only print) the ouptput of an included PHP script which you cannot alter, you have to do an ugly workaround:

<?php

ob_start();
include 'date.php';
$date = ob_get_contents();
ob_end_clean();

$str = "Today is {$date}.";

?>
忘你却要生生世世 2024-11-26 22:02:48

这是没有 \e eval 的解决方案:

$str = "{include 'hello.php'}Today is {include 'date.php'}.";

echo preg_replace_callback("/\{include '(.*?)\'}/", function($m) {
  return include($m[1]);
}, $str);

hello.php

<?php return "Hello!\n"; ?>

date.php

<?php return date('jS \of F'); ?>, 2011

结果: Hello!今天是 7 月 21 日。

Here is the solution without \e eval:

$str = "{include 'hello.php'}Today is {include 'date.php'}.";

echo preg_replace_callback("/\{include '(.*?)\'}/", function($m) {
  return include($m[1]);
}, $str);

hello.php

<?php return "Hello!\n"; ?>

date.php

<?php return date('jS \of F'); ?>, 2011

result: Hello! Today is 21st of July.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文