如何使用php preg_replace替换HTML标签 [英] How to use php preg_replace to replace HTML tags
本文介绍了如何使用php preg_replace替换HTML标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用<code>
更改<pre>
,用</code>
更改</pre>
.
我在使用斜杠/和正则表达式时遇到问题.
I'm having problem with the slash / and regex.
推荐答案
您可以只使用str_replace:
You could just use str_replace:
$str = str_replace(array('<pre>', '</pre>'), array('<code>', '</code>'), $str);
如果您不得不使用正则表达式:
If you feel compelled to use regexp:
$str = preg_replace("~<(/)?pre>~", "<\\1code>", $str);
如果要单独替换它们:
$str = preg_replace("~<pre>~", '<code>', $str);
$str = preg_replace("~</pre>~", '</code>', $str);
您只需要转义该斜线即可.
You just need to escape that slash.
这篇关于如何使用php preg_replace替换HTML标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文