我有一根这样的绳子:
[<span style="font-size: 12.1599998474121px; line-height: 15.8079996109009px;">heading </span>heading="h1"]Its a <span style="text-decoration: line-through;">subject</span>.[/<span style="font-size: 12.1599998474121px; line-height: 15.8079996109009px;">heading</span>]我想使用PHP、preg_replace等删除括号内的HTML标记。最后的字符串应该如下所示:
[heading heading="h1"]Its a <span style="text-decoration: line-through;">subject</span>.[/heading]我寻找了很多解决办法,但都没有成功。
发布于 2015-05-10 08:35:16
这应该适用于你:
在这里,我只在字符串的每个括号中使用strip_tags()并返回它。
echo $str = preg_replace_callback("/\[(.*?)\]/", function($m){
return strip_tags($m[0]);
}, $str);发布于 2015-05-10 08:35:33
您可以使用带有以下正则表达式的回调并利用strip_tags() .
$str = preg_replace_callback('~\[[^]]*]~',
function($m) {
return strip_tags($m[0]);
}, $str);发布于 2015-05-10 08:33:38
这取决于你想要移除多少。
示例:
Pattern: '<.*?>'
Result: [heading heading="h1"]Its a subject.[/heading]但从您的回答来看,您希望保留标题中的html标记。我不明白,究竟是根据哪条规则?为什么这是个例外?
https://stackoverflow.com/questions/30149252
复制相似问题