This is a difficult question for me to word correctly, but I am trying to dynamically insert an ASIDE [specifically, just a "special thanks" note] between paragraphs. Initially I decided to drop this after the second paragraph by using substr_count(). I am floating this block, so if all it had to deal with was textual content there was no issue word-wrapping it. However, if it ran adjacent to an image or a PRE or anything else, it got wonky.
Anyway, what I want to do is detect when there is the first occurrence of TWO adjacent paragraphs and insert my aside between those. I.e.:
<p> Here is the first paragraph. </p>
<aside> INSERT THIS HERE </aside>
<p> Here is the second paragraph. </p>
Thoughts are appreciated.
Update: the substr_count() I am currently using.
Because I got voted down for not showing the original code, I'll post it below. I am using Wordpress but this isn't a WP specific question, as I'm taking the_content() as the string, counting the occurrences of P, and inserting the custom field there. This is ultimately not what I want to do, but I want to count two concurrent P's and insert this field between. It may be formatted strangely, as of course when I c/p from my editor stuff was all over the place.
$thanks = get_post_meta(get_the_ID(), 'thanks', true);
if (!$thanks) {
the_content();
}
else {
$show_after_p = 0;
$content = apply_filters('the_content', $post->post_content);
if(substr_count($content, '<p>') > $show_after_p) {
$contents = explode("</p>", $content);
$p_count = 0;
foreach($contents as $content)
{
echo $content;
if($p_count == $show_after_p)
{
?>
<aside class="thanks clearfix">
<p>
<?php echo '<span>Special thanks: </span>'.$thanks; ?>
</p>
</aside>
<?php }
echo "</p>";
$p_count++;
}
}
}
?>
Why not JS? Well, javascript could definitely do this, but if I can do this in PHP then yahtzee.