View Full Version : Insert Ad Code Within Page
Chris
06-21-2006, 11:46 AM
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.
KelliShaver
06-21-2006, 12:29 PM
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....
// where $article is... erm... your article. ;)
$arr = explode("</p>", $article);
$arr[1] = "your add code here".$arr[1];
$article = implode("</p>", $arr);
echo $article;
This should do what you are after:
$content = preg_replace('<\</p\>>', '</p>'.$square_ad, $content, 1);
Just appends '$square_ad' after the first '</p>'. In my case, '$square_ad' was Adsense code.
Blue Cat Buxton
06-21-2006, 01:16 PM
Cool, I was trying to do something like this last night!
Chris
06-21-2006, 01:18 PM
I assume with yours I could just change the final argument to be whichever paragraph I want?
Looking at the preg_replace page (http://uk.php.net/preg_replace), 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:
$arr[3] = "your add code here".$arr[3];
would put it after third I believe.
KelliShaver
06-21-2006, 05:09 PM
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:
$arr[2].="yoour ad code";
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.
Powered by vBulletin® Version 4.2.2 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.