Anyone have preexisting code to find the first occurence of </p> in an article and then insert adcode after it? Don't want to reinvent the wheel if I don't have to.
Anyone have preexisting code to find the first occurence of </p> in an article and then insert adcode after it? Don't want to reinvent the wheel if I don't have to.
Eh, there's probably a more efficient way of doing it (but regular expressions confuse the hell out of me), but off the top of my head....
Code:// where $article is... erm... your article. ;) $arr = explode("</p>", $article); $arr[1] = "your add code here".$arr[1]; $article = implode("</p>", $arr); echo $article;
Last edited by KelliShaver; 06-21-2006 at 12:32 PM.
This should do what you are after:
Just appends '$square_ad' after the first '</p>'. In my case, '$square_ad' was Adsense code.Code:$content = preg_replace('<\</p\>>', '</p>'.$square_ad, $content, 1);
Cool, I was trying to do something like this last night!
Looking at the preg_replace page, it looks like changing it to 3 for example would put an ad after first, second and third - not just the third.
KelliShaver's should be easier to do something like that, eg:
would put it after third I believe.Code:$arr[3] = "your add code here".$arr[3];
Yep. What it's doing is appending it to the beginning of the 4th paragraph (the array count starts at 0).
You could also do:
to append it to the end of the 3rd. It really makes no difference which you do, and I really don't know why I did it one way and not the other in the example above.Code:$arr[2].="yoour ad code";
Last edited by KelliShaver; 06-21-2006 at 05:13 PM.
Bookmarks