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"; } }
Output for 7.0.0 - 7.0.25, 7.1.0 - 7.1.20, 7.2.6 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
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>

preferences:
148.24 ms | 408 KiB | 5 Q