Hi, I've got $r['fulldescription'] which stores a long bit of text. Basically I wan't to show two ad blocks in that text. I need to show an adblock every 250 words but limit the amount of adblocks to 2. Anyone got any ideas, thanks!
If I understood you right, then you should use substr(); It helps you to limit the words. substr($string,0, number of letters);
Try this code. It's pretty inefficient, but it does the job. <?php $string = 'This is a test string with some words in it'; $Advert = '{ad}'; $Frequency = 4; $WordCounter = 1; $WordArray = explode( ' ', $string ); for ( $count = 0 ; $count <= sizeof( $WordArray )-1 ; $count++ ) { echo $WordArray[ $count ] . ' '; if ( $WordCounter++ == $Frequency ) { $WordCounter = 1; echo $Advert; } } ?> PHP: Brew
<?php $string = ''; $advert = ''; function insert_ads($string,$advert) { $arr = explode(" ",$string); if(count($arr) >= 250) { $arr[249] = $arr[249].$advert; } if(count($arr) >= 500) { $arr[499] = $arr[499].$advert; } return implode(" ", $arr) } echo insert_ads($string,$advert); ?> PHP: Not sure if it is the best way, but this is something I would use to do what your asking.