剪切 HTML 标签并重新包装 HTML 标签 Part/2 [英] Cut HTML Tags and wrap HTML tags again Part/2
本文介绍了剪切 HTML 标签并重新包装 HTML 标签 Part/2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可以在当前父标签的每个子标签中映射标签级联,像这样
its possible to map the cascade of tags in each child-tag of current parent tag, like this
来自:
<p>
string
<b>
bold
<em>italic string</em>
also(bold)
</b>
</p>
致:转换为这个字符串
<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 部分)由 jerry 回答剪切 HTML 标签并再次包装 HTML 标签 Part/1
the base question (Part 1) was answered by jerry Cut HTML Tags and wrap HTML tags again Part/1
我认为正则表达式是我的朋友,在这种情况下也是如此:)
i think regex is my friend, also in this case :)
推荐答案
解决方案
来源
$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 每一行...
grep each lines ...
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);
这篇关于剪切 HTML 标签并重新包装 HTML 标签 Part/2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文