可以映射当前父标记的每个子标记中的标记级联,如此
FROM:
<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>
杰里回答了基本问题(第1部分) Cut HTML Tags and wrap HTML tags again Part/1
我认为正则表达式是我的朋友,在这种情况下:)
答案 0 :(得分: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>
';
Init vars ...
$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);