$str ="<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<h2></h2>
<p></p>
<p></p>";
$i = 0;
$str = preg_replace_callback("/<\/p>/u", function($match) use(&$i){
$i ++;
if($i % 2 == 0){
return "</p><div></div>";
}
return "</p>";
}, $str);
echo $str;
<script type="text/javascript">
$(document).ready(function(){
$("p:eq(1)").after('<ins class="adsbygoogle" style="display: block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-XXXXXXXXXX" data-ad-slot="XXXXXXX"></ins>');
});
</script>
<script src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<script>
$(document).ready(function(){
(adsbygoogle = window.adsbygoogle || []).push({});
});
</script>