Reputation: 2547
How can I wrap from the beginning of the text in a string until the first <p>
appears? For example, If the string is
this is some <b>text</b><p>Beginning of a paragraph</p>
I want
<p>this is some <b>text</b></p><p>Beginning of a paragraph</p>
Any way to achieve this? Thanks!
Upvotes: 1
Views: 65
Reputation: 27723
If our inputs would be just as simple as the one in the question, we would start with a simple expression and 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 visualizes regular expressions:
Upvotes: 0
Reputation: 51894
Perhaps try
$str = '<p>' . preg_replace('#<p>#', '</p>\0', $str, 1);
Upvotes: 2
Reputation: 196
Maybe use something like this:
str_replace(".","</p>.<p>", $myText);
Upvotes: 0