When adding Adsense box to your website there are a lot of choices. You can put it on the banner, or the sidebar or inside the content, for the first and second its trivial to do, this post talking about the third one, inside post content, nowadays most publisher choose to put Adsense right inside the content.

The simplest solution is splitting the content using </p> as delimiter.

 
<?php
function adsense_skyrocket(){
?>
            <div style="position:absolute;left:647px;top:35px;">
            <script type="text/javascript"><!--
google_ad_client = "your client id";
 
google_ad_slot = "slot number";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
            </div>  
<?php
}    
 
function adsense_rectangle(){
?>
            <div class="post">
            <script type="text/javascript"><!--
google_ad_client = "your client id";
google_ad_slot = "slot number";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
            </div>
 
<?php
}
 
 
function insertads_middle($content)
{   
    $content = explode("</p>", $content);
    $paragraphAfter = floor(count($content) / 2); 
    $new_content = '';
    for ($i = 0; $i < count($content); $i++) {
        echo $content[$i] . "</p>";
        if ($i == $paragraphAfter) {
            adsense_rectangle();
        }
    }
}
 

Split content with different delimiters

Sometimes, the contents not only contains the paragraphs, it may also contains div or blockquote and other types of content sections. You may need to split the content with multiple delimiter string. There are no easy solution in PHP to do this, because after the splitting we still need to combine them together with the original delimiter.

Here is a solution, we don't use explode function, but the preg_match_all

 
function insertads_middle_multi($content, $delimiters, $replace)
{
    preg_match_all('~' . implode("|", $delimiters) . '~', $content, $matches, PREG_OFFSET_CAPTURE);
    $afterSection = floor(count($matches[0]) / 2);
    echo substr_replace($content, $matches[0][$afterSection][0] . $replace, $matches[0][$afterSection][1], strlen($matches[0][$afterSection][0]));    
}
 

The idea is using regular expression to find each delimiter and their position in the content, with the position information we can replace the specified nth delimiter in the content.

Here is how to use it

 
function adsense_rectangle_return(){
    ob_start();
    adsense_rectangle();
    $ret = ob_get_clean();
    return $ret;
}
 
insertads_middle_multi($this->store[0]->contents, array("</p>","</div>"), adsense_rectangle_return())
 

We need to replace the delimiter as our ads, so the HTML code of the ads should be returned as a string. The delimiters array will be imploded with "|" which means "or" in regular expression, you can add more delimiter if needed.