用 <p> 换行直到第一个 <p> 出现
Wrap text with <p> until the first <p> appears
如何从字符串中的文本开头开始换行,直到第一个 <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_replace(".","</p>.<p>", $myText);
也许试试
$str = '<p>' . preg_replace('#<p>#', '</p>[=10=]', $str, 1);
如果我们的输入与问题中的输入一样简单,我们将从一个简单的表达式和一个 preg_replace
开始:
$re = '/(.+?)(<p>.+?<\/p>)/m';
$str = 'this is some <b>text</b><p>Beginning of a paragraph</p>';
$subst = '<p><\/p>';
$result = preg_replace($re, $subst, $str);
echo $result;
Demo
输出
<p>this is some <b>text</b><\/p><p>Beginning of a paragraph</p>
正则表达式电路
jex.im 可视化正则表达式:
如何从字符串中的文本开头开始换行,直到第一个 <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_replace(".","</p>.<p>", $myText);
也许试试
$str = '<p>' . preg_replace('#<p>#', '</p>[=10=]', $str, 1);
如果我们的输入与问题中的输入一样简单,我们将从一个简单的表达式和一个 preg_replace
开始:
$re = '/(.+?)(<p>.+?<\/p>)/m';
$str = 'this is some <b>text</b><p>Beginning of a paragraph</p>';
$subst = '<p><\/p>';
$result = preg_replace($re, $subst, $str);
echo $result;
Demo
输出
<p>this is some <b>text</b><\/p><p>Beginning of a paragraph</p>
正则表达式电路
jex.im 可视化正则表达式: