3v4l.org

run code in 300+ PHP versions simultaneously
<?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"; } }

preferences:
23.58 ms | 405 KiB | 5 Q