У меня есть небольшой скрипт, который заменяет некоторый текст из файла 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;
}
Я уверен, что хорошо выполненное регулярное выражение может быть хорошей заменой.