<?php
$input= "<p>text text text</p>
<p>text text text</p>
<p>text text text</p>
<p>text text text</p>
<p>text text text</p>
<p>text text text</p>";
$arr =explode(PHP_EOL, $input);
$adsbeforeparagraph = array(0,2,4); // subtracted 1 to suit normal array numbering
$ad = 'ADS CODE';
Foreach($arr as $key => $line){
If(in_array($key, $adsbeforeparagraph)){ // is this a line that should have Ads?
Echo $ad . $line . "\n";
}else{
Echo $line . "\n";
}
}
ADS CODE<p>text text text</p>
<p>text text text</p>
ADS CODE<p>text text text</p>
<p>text text text</p>
ADS CODE<p>text text text</p>
<p>text text text</p>