Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
替换php中嵌套标记中的文本_Php_Regex_Preg Replace - Fatal编程技术网

替换php中嵌套标记中的文本

替换php中嵌套标记中的文本,php,regex,preg-replace,Php,Regex,Preg Replace,我需要替换一些文本。 为此,我需要一个regexp模式。全文如下: <div CLASS="CHAP_BM_CON">Outer <div CLASS="CHAP_BM_FIRST">DIV inner</div> Outer ends here</div><div CLASS="CHAP_BM_FIRST">Separate DIV Even inner</div> 外部分区内部分区 外端与内端分开 在上面的段落中,

我需要替换一些文本。 为此,我需要一个regexp模式。全文如下:

<div CLASS="CHAP_BM_CON">Outer <div CLASS="CHAP_BM_FIRST">DIV inner</div> 
Outer ends here</div><div CLASS="CHAP_BM_FIRST">Separate DIV Even inner</div>
外部分区内部分区
外端与内端分开
在上面的段落中,我们可以看到一个嵌套的DIV。我需要reg exp模式来满足如下输出:

<div CLASS="CHAP_BM_CON">Outer <div CLASS="CHAP_BM_FIRST">DIV inner <!--CHAP_BM_FIRST--></div> 
Outer ends here<!--CHAP_BM_CON--></div><div CLASS="CHAP_BM_FIRST">Separate DIV Even     inner<!--CHAP_BM_FIRST--></div>
外部分区内部分区
外端与内端分开

如何为这种情况编写regexp?

我尝试了下面的代码

<?php
function parseTagsRecursive($input)
{

 $regex = '/<div tagname="(.*?)" (.*?)>(.*?)<\/div>/si';    
if (is_array($input)) {

    $input = '<div tagname='.$input[1].' '.$input[2].'>'.$input[3].'<!--'.$input[1].'--></div>';

}   

return preg_replace_callback($regex, 'parseTagsRecursive', $input);

}

$content = '<div CLASS="CHAP_BM_CON">Outer <div CLASS="CHAP_BM_FIRST">DIV inner</div> 
Outer ends here</div><div CLASS="CHAP_BM_FIRST">Separate DIV Even inner</div';


echo $ver = parseTagsRecursive($content);