1

它可以映射当前父标签的每个子标签中的标签级联,像这样

从:

<p>
     string
    <b>
      bold
        <em>italic string</em>
      also(bold)
    </b>
 </p>

TO:转换为这个字符串

  <p>
    string
  </p>

        <b p><!--------------------------------------- insert -->
          bold
        </b p><!--------------------------------------- insert -->

            <em b p>italic string</em b p><!----------- insert -->

        <b p> <!--------------------------------------- insert -->
          also(bold)
        </b p><!--------------------------------------- insert -->

   <p>
   </p>

jerry Cut HTML Tags 回答了基本问题(第 1 部分)并 再次包装 HTML 标签 Part/1

我认为正则表达式是我的朋友,在这种情况下也是如此:)

4

1 回答 1

0

解决方案

资源

$page = '
<u>
str
<b>
bold
<em>
ital
<strike>
ic stri
</strike>
ng
</em>
also(bold)
</b>
</u>
<u>
str
<b>
bold
also(bold)
</b>
</u>
';

初始化变量...

$o = 'open';
$c = 'close';
$n = 'node';

$tag = null;
$tagName = null;
$addNode = null;
$strTmp = null;

grep 每一行...

foreach(preg_split("/((\r?\n)|(\r\n?))/", $page) as $line){
    $lines[] = $line;
}

替换逻辑...

for($i=0;$i<count($lines);$i++) {

    $line = $lines[$i];
    preg_match ('/<([^\/<]*?)>/' , $line, $open);
    preg_match ('/<(\/[^<]*?)>/' , $line, $close);

    $str = ""; 

    if($tag == $o ){

        if($addNode) {
            $str .= "#".$addNode."#";
        }
        $str .= $line;

        preg_match ('/<(\/[^<]*?)>/' , $lines[$i+1], $m);
        if(!strpos(@$m[1], $tagName)) {
            $addNode .= $tagName." ";
        }
    }

    if($tag == $c ){
        $str .= $line;
        $addNode = null;
    }

    if($tag == $n ){
        $str .= $line;
    }

    //$line = $addNode.$line;

    if(count($open)>0){$tag = $o; $tagName = $open[1];}
    if(count($close)>0){$tag = $c;}
    if(count($open)<1 && count($close)<1 ){$tag = $n;}

    $strTmp .= $str;

}

打印 ...

echo $strTmp = preg_replace('(<([\w]+)>#([^#]*)#)' , "<$1 $2>", $strTmp);
于 2013-09-27T13:15:01.037 回答