剪下HTML标签并重新包装HTML标签Part / 1 [英] Cut HTML Tags and wrap HTML tags again Part/1
本文介绍了剪下HTML标签并重新包装HTML标签Part / 1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试转换后续字符串
< p>
字符串
< b>
bold
< em>斜体字符串< / em>
也(粗体)
< / b>
< / p>
添加到此字符串中
< p>
字符串
< / p> <!--------------------------------------- insert - >
< b>
bold
< / b> <!------------------------------------ insert - >
< em>斜体字符串< / em>
< b> <!------------------------------------ insert - >
也(粗体)
< / b>
< p> <!---------------------------------------- insert - >
< / p>
我知道这不是有效的html,但我需要这个用于我的项目。
...正则表达式是一种很好的方式,但我只是没有解决。
整个结构都是动态的,
我的想法是,regex
// open tag close
$ regex ='如果($ 1!= $ 3){
$ newString =<(。*?)>(。*?)< /(。*?)>'
, $ 1 GT; $ 2'; / $ 1 GT;< / $ 3 GT;
}
但不起作用!
那么,我认为有效的做法是两次连续的替换:
1。
^(\s * <(?!/))([^ <+)>(?= [^ < ()()()()()()()() *< / \ 2>)
替换为:
$ 1 $ 2> $ 3 \ $ 1 / $ 2>
<2>
/ [^>] +> $ \\\
\K([^<>] *)(^ \s *)(= LT;?/([^>] +)>)
换成:
$ 2 <$ 3> \\\
$ 1 $ 2
regex101 demo $ b
我不确定ideone为什么将<> ;
转换为& lt;
和& gt;
,我不知道如何避免它:($ / b
$ b
但是,这与您的示例。
i try to convert the follow string
<p>
string
<b>
bold
<em>italic string</em>
also(bold)
</b>
</p>
to this string
<p>
string
</p> <!--------------------------------------- insert -->
<b>
bold
</b> <!------------------------------------ insert -->
<em>italic string</em>
<b> <!------------------------------------ insert -->
also(bold)
</b>
<p> <!---------------------------------------- insert -->
</p>
i know that is not valid html, but i need this for my project. ... regex its a good way, but i just do not get solved.
the whole strcture will be dynamic,
my thinking was, regex
// open tag close
$regex = '<(.*?)>(.*?)</(.*?)>'
if ($1 != $3) {
$newString = <$1>$2</$1></$3>
}
but don't work!
解决方案
Well, something which I think works is two successive replaces:
1.
^(\s*<(?!/))([^<]+)>(?=[^<]+\n)((?:(?!</\2>)[\s\S])*?)(?=\n *<)(?![^>]*</\2>)
Replace by:
$1$2>$3\n$1/$2>
2.
/[^>]+>$\n\K([^<>]*?)(^\s*)(?=</([^>]+)>)
Replace by:
$2<$3>\n$1$2
I'm not sure why ideone turns the <>
into <
and >
and I don't know how to avoid it :(
But that works with your sample.
这篇关于剪下HTML标签并重新包装HTML标签Part / 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文