假设我有一个包含文件foo.php,其包含文本“此文件是foo”。
因此,当我使用<?php include 'foo.php'; ?>
加载包含时,我希望能够在显示它所包含的文本之前替换包含中的字符串。
例如,我想用“cool”替换包含中的“foo”。
明白了吗?
<?php
function callback($buffer)
{
return (str_replace("foo", "cool", $buffer));
}
ob_start("callback");
include 'foo.php';
ob_end_flush();
?>
$content=includeFileContent('test.php');
echo str_replace('foo', 'cool', $content);
function includeFileContent($fileName)
{
ob_start();
ob_implicit_flush(false);
include($fileName);
return ob_get_clean();
}
<?php
include 'foo.php';
$output = file_get_contents('bar.php');
$output = processContent($output);
?>
foo.php的示例:
<?php
function processContent($pText){
return str_replace('foo','cool',$pText);
}
?>
file_get_contents()
函数来读取纯文本文件呢? - Nev Stokes