I have no examples in mind for this, it's just for the sake of pondering along the same lines as my other thread: http://forums.digitalpoint.com/showthread.php?t=1453518 Assuming you have dynamic text, say $text, like in Wordpress or Drupal etc. Is there a way in php to insert some code, an image or ad or whatever, after so many words, say after 300 words add a logo for example. Or how about adding some code after every 3 </p> tags, like an ad of wesite name or something? I guess you could the first one sort of using the code from then other thread, but is there a different way? I way to count something, like </p> tags, up to a certain number then, bam, in goes your snippet of code? Not even sure if this makes sense as it's very late here?
Not exactly sure what you mean but you can count the characters then after X characters insert a code like so... $text = 'long string of whatever you want'; $text2 = str_split($text, 50); //every 50 characters $ad = 'html or text of ad here'; echo"".$text2[0].""; echo"".$text2[1].""; echo"".$text2[2].""; echo"".$ad.""; //the ad of the website echo"".$text2[3].""; echo"".$text2[4].""; echo"".$text2[5].""; echo"".$text2[6].""; //would display 7x50 = 350 character keep adding as many as you want...ie up to text[999999] PHP: as for if you want to do it after every </p> or whatever you need to look into explode or implode...hope this helped.
Might not be the most efficient, but it's probably the easiest way to do the tag thing. <?php $tag_count = 0; function id_tag($tag) { global $tag_count; $tag_count++; return "</{$tag[1]}:$tag_count>"; } $str = '<p>paragraph one</p><p>paragraph two</p><p>paragraph three</p><p>paragraph four</p>'; $str = preg_replace_callback('#</([a-z]+)>#si', 'id_tag', $str); $str = str_replace('</p:3>', '</p:3>INSERTED', $str); $str = preg_replace('#</([a-z]+):\d+>#si', '</$1>', $str); echo "$str\n"; ?> PHP:
I was half asleep when I made the thread, had to read it again now. Rep added to both. PHP is so cool, and slightly mad, for the things you can do. Edit: I just had another thought. With AustinQPT's example, can you count backwards from the end of the string? Say if you only wanted to put one lot of code in for an advertisment or your logo just once 100 chracters from the end of the text. I know you can find the character with something like $text[-100]?
I just realized it would probably be easier still to just keep track of the counter inside of id_tag, and return the code tag with the stuff attached if the counter is right. That would get rid of the str_replace and last preg_replace.
Jobert, I can get yours to work as non dynamic text as you've posted, but if I try and swap $str = '<p>paragraph one</p><p>paragraph two</p><p>paragraph three</p><p>paragraph four</p>'; with: $str = $this->item->text; i.e. as it appear in a CMS it just displays the text as normal without the INSERTED bit.