如何将带有属性的标签替换为字符串空?

时间:2016-10-02 12:00:51

标签: php regex

我如何从以下字符串替换字符串:

"this is a <p id="1" /> text <p id="2" /> of a string"

要:

"this is a <br> text <br> of a string" ?

我想要用<p ... />替换字符串中的所有<br>

3 个答案:

答案 0 :(得分:2)

更新 :(仅<p ... />

echo preg_replace("/<p[^\/]*\/>/i", "<br />", 'this is a <p id="1" /> text <p id="2" /> of a string');

答案 1 :(得分:1)

您可以将preg_replace()str_replace()

一起使用
$newString = preg_replace("/<p[^>]*?>/", "", $yourString); // will remove all starting tag
$addNewLine = str_replace("</p>", "<br />", $newString); // will replace closing tag with <br>

答案 2 :(得分:0)

试一试:

<?php
echo preg_replace("<p id=\"d\"/>", "<br>", "this is a <p id=\"1\" /> text <p id=\"2\" /> of a string");
?>

你会得到:

this is a

text

of a string