我想改<pre>
与 <code>
和 </pre>
与 </code>
.
我在使用斜杠/和正则表达式时遇到问题。
最佳答案
你可以只使用 str_replace:
$str = str_replace(array('<pre>', '</pre>'), array('<code>', '</code>'), $str);
如果您不得不使用正则表达式:
$str = preg_replace("~<(/)?pre>~", "<\\1code>", $str);
如果要分别替换它们:
$str = preg_replace("~<pre>~", '<code>', $str);
$str = preg_replace("~</pre>~", '</code>', $str);
你只需要转义那个斜杠。
关于php - 如何使用 php preg_replace 替换 HTML 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3376051/