RegExp替换PHP中的特定XML/HTML标记

RegExp替换PHP中的特定XML/HTML标记,php,xml,regex,tags,Php,Xml,Regex,Tags,我有一个小脚本,用于替换来自xml文件的一些文本,这是一个示例: <b>Hello world,</b> <include file="dynamiccontent.php" /> <img src="world.png" /> a lot of <i>stuff</i> 你好,世界, 很多东西 很明显,字符串要长得多,但我想在使用查找的分解时,用文件名中的脚本内容替换 <include file=" 已编辑以允

我有一个小脚本,用于替换来自xml文件的一些文本,这是一个示例:

<b>Hello world,</b>
<include file="dynamiccontent.php" />
<img src="world.png" />
a lot of <i>stuff</i>
你好,世界,
很多东西
很明显,字符串要长得多,但我想在使用查找的分解时,用文件名中的脚本内容替换

<include file="

已编辑以允许多个包含和文件检查

$content = '<b>Hello world,</b>
<include file="dynamiccontent.php" />
<img src="world.png" />
a lot of <i>stuff</i>';

preg_match_all('!<include file="([^"]+)" />!is', $content, $matches); 
if(count($matches) > 0)
{
    $replaces = array();
    foreach ($matches[1] as $file)
    {
        $tag = '<include file="'.$file.'" />';
        if(is_file($file) === true)
        {   
            ob_start();
            require $file;
            $replaces[$tag] = ob_get_clean();
        } 
        else
        { 
            $replaces[$tag] = '{Include "'.$file.'" Not Found!}';
        }
    } 
    if(count($replaces) > 0)
    {
        $content = str_replace(array_keys($replaces), array_values($replaces), $content);
    }
}

echo $content;
$content='Hello world,
很多东西';

preg_match_all('!已编辑以允许多个包含和文件检查

$content = '<b>Hello world,</b>
<include file="dynamiccontent.php" />
<img src="world.png" />
a lot of <i>stuff</i>';

preg_match_all('!<include file="([^"]+)" />!is', $content, $matches); 
if(count($matches) > 0)
{
    $replaces = array();
    foreach ($matches[1] as $file)
    {
        $tag = '<include file="'.$file.'" />';
        if(is_file($file) === true)
        {   
            ob_start();
            require $file;
            $replaces[$tag] = ob_get_clean();
        } 
        else
        { 
            $replaces[$tag] = '{Include "'.$file.'" Not Found!}';
        }
    } 
    if(count($replaces) > 0)
    {
        $content = str_replace(array_keys($replaces), array_values($replaces), $content);
    }
}

echo $content;
$content='Hello world,
很多东西';

preg_match_all('!那么您想知道元素的属性文件的值包括什么?请尝试:

$sgml = <<<HTML
<b>Hello world,</b>
<include file="dynamiccontent.php" />
<img src="world.png" />
a lot of <i>stuff</i>
HTML;

preg_match('#<include file="([^"]+)"#',$sgml,$matches);

print_r($matches[1]); // prints dynamiccontent.php

$sgml=那么您想知道元素的属性文件的值是多少?试试:

$sgml = <<<HTML
<b>Hello world,</b>
<include file="dynamiccontent.php" />
<img src="world.png" />
a lot of <i>stuff</i>
HTML;

preg_match('#<include file="([^"]+)"#',$sgml,$matches);

print_r($matches[1]); // prints dynamiccontent.php

$sgml=
/(?
/(?是的,这很好,但在这种情况下,如果您看到字符串包含一些html标记和文本,我只能提取文件名,而不加载其他内容..mh..我可以用简单的替换替换替换include..:是的,这很好,但在这种情况下,如果您看到字符串,我只能提取文件名,而不加载其他内容包含一些html标记和文本..mh..我可以用一个简单的替换来替换include..:s