如何从字符串中的文本开头换行直到第一个<p>
出现?例如,如果字符串是
this is some <b>text</b><p>Beginning of a paragraph</p>
我想
<p>this is some <b>text</b></p><p>Beginning of a paragraph</p>
有什么办法可以做到这一点?谢谢!
如何从字符串中的文本开头换行直到第一个<p>
出现?例如,如果字符串是
this is some <b>text</b><p>Beginning of a paragraph</p>
我想
<p>this is some <b>text</b></p><p>Beginning of a paragraph</p>
有什么办法可以做到这一点?谢谢!
也许尝试
$str = '<p>' . preg_replace('#<p>#', '</p>\0', $str, 1);
也许使用这样的东西:
str_replace(".","</p>.<p>", $myText);
如果我们的输入与问题中的输入一样简单,我们将从一个简单的表达式和 a 开始preg_replace
:
$re = '/(.+?)(<p>.+?<\/p>)/m';
$str = 'this is some <b>text</b><p>Beginning of a paragraph</p>';
$subst = '<p>$1<\/p>$2';
$result = preg_replace($re, $subst, $str);
echo $result;
<p>this is some <b>text</b><\/p><p>Beginning of a paragraph</p>
jex.im可视化正则表达式: