我如何从以下字符串替换字符串:
"this is a <p id="1" /> text <p id="2" /> of a string"
要:
"this is a <br> text <br> of a string" ?
我想要用<p ... />
替换字符串中的所有<br>
。
答案 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