【发布时间】:2010-09-28 08:16:31
【问题描述】:
我有一个小脚本可以替换一些来自 xml 文件的文本,这是一个示例:
<b>Hello world,</b>
<include file="dynamiccontent.php" />
<img src="world.png" />
a lot of <i>stuff</i>
显然字符串要长得多,但我想用文件名中脚本的内容替换<include
file="*" />,当时我使用了一个发现
<include file="
但我认为有更好的方法来解决它。 这是我的代码:
$arrContent = explode("<include ", $this->objContent->content);
foreach ($contenuto as $piece) { // Parsing array to find the include
$startPosInclude = stripos($piece, "file=\""); // Taking position of string file="
if ($startPosInclude !== false) { // There is one
$endPosInclude = stripos($piece, "\"", 6);
$file = substr($piece, $startPosInclude+6, $endPosInclude-6);
$include_file = $file;
require ($include_file); // including file
$piece = substr($piece, $endPosInclude+6);
}
echo $piece;
}
我确信一个做得好的正则表达式可以是一个很好的替代品。
【问题讨论】: