1

如何从字符串中的文本开头换行直到第一个<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>

有什么办法可以做到这一点?谢谢!

4

3 回答 3

2

也许尝试

$str = '<p>' . preg_replace('#<p>#', '</p>\0', $str, 1);
于 2019-06-06T17:39:56.713 回答
0

也许使用这样的东西:

str_replace(".","</p>.<p>", $myText);
于 2019-06-06T17:33:16.320 回答
0

如果我们的输入与问题中的输入一样简单,我们将从一个简单的表达式和 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可视化正则表达式:

在此处输入图像描述

于 2019-06-06T17:40:58.720 回答