Reputation: 2845
I'm trying to insert some text midway through some HTML content using the following regex:
if (preg_match('/(.*<\/a><\/p>)(.*?)/Ui', $content, $match))
$custom_content = $match[1] . $title . $match[2];
The result I want is that $title
is inserted into the HTML immediately after the first </a></p>
is encountered. This much is working, however the second capture group always remains empty, regardless of the greedy / ungreedy combinations I use. The result is that the remainder of the content after the </a></p>
is not appended to the result.
What am I doing wrong here?
Upvotes: 0
Views: 403
Reputation: 2845
Ironically I found the answer 30 seconds after posting this... adding the 's' option to the pattern matches new lines as well:
/(.*<\/a><\/p>)(.*?)/Uis
Thanks anyway :-)
Upvotes: 0
Reputation: 164766
Why bother capturing the part you don't care about. Simply replace the first group with itself plus $title
, eg
$custom_content = preg_replace('@(</a></p>)@Ui', '$1' . $title, $content, 1);
Upvotes: 1