I'm trying trying to use to insert content after the nth html paragraph that doesn't contain an image. So far I haven't been able to properly exclude paragraphs containing images.
What am I missing or is this outside the effective use of regex?
My code so far:
$content = '
<p><a href="#"> <img align="right" src="blah.jpg"> </a> Some paragraph text</p>
<param name="blah" value="blah"> <!-- to make sure we are only counting <p>s -->
<param name="blah" value="blah">
<param name="blah" value="blah">
<p>First paragraph to count.</p>
<p>Second paragraph to count.</p>
<p>Blah blah <a href="#">link</a><img src="blah.jpg" /> blah </p>
<p>Third paragraph to count.</p>
<p>Fourth paragraph to count.</p>
';
$insert = "\n\n".'<h3>INSERT ME</h3>'."\n\n";
$pattern = '/((?:.*?<p[\W.]*?>(?!<img)){3})(.*$)/is';
preg_match($pattern, $content, $matches);
if (!empty($matches)) {
echo "Yes!\n";
echo $matches[1].$insert.$matches[2];
}else{
echo "No.\n";
echo $content;
echo $insert;
}
Thanks!