替换 PHP 包含文件中的字符串

2022-01-25 00:00:00 include php

Let's say I have an include file, foo.php, and it contains the text "this file is foo".

So, when I load the include using <?php include 'foo.php'; ?>, I want to be able to replace a string from the include before displaying the text it contains.

So let's say I want to replace "foo" from the include with "cool".

Make sense?

解决方案

You can use this:

<?php

function callback($buffer)
{
  return (str_replace("foo", "cool", $buffer));
}
ob_start("callback");
include 'foo.php';
ob_end_flush();

?> 

相关文章