On 22 Oct 2008 at 6:34, Ron Piggott wrote: > I am tweaking a blog application I have programmed. I am trying to > display a Google ad half through the blog entry, at the first available > <br />. > > The code I use so far is: > > $half_way = strlen( nl2br(stripslashes($entry))) /2 ; > $ad_position = strpos ( nl2br(stripslashes($entry)) , "<br />" , > $half_way ); > echo substr( nl2br(stripslashes($entry)) , 0, $ad_position); > > Is there a way to modify my strpos syntax to check and see if the > nearest <br /> is before the half way mark? > > What is tending to happen is the ad is being placed 5/7ths of the way > through the blog entry because of the length of the paragraph the half > way character falls in. Visually it doesn't look balanced. I would > prefer the ad display 4/7th of the way through the blog entry in those > situations. > > Thanks for helping me. Hi, Disclaimer: Without seeing the actual blog entry this is all guess work! Your code above seems to find the half way point in the raw text. This is all very well if you do not have paragraphs or other formatting code that can move text around once displayed. To overcome this you will have to try and detect the number of paragraphs (or formatting code) before the half way point and after it and try and move the Google Ad entry to accommodate this. This will involve trial and error to determine an algorithm that best matches your blog entries. Or you could go down the easy route and add some sort of marker (database entry or a tag of some sort) to each blog which indicates were the Google ads should go. Personally I prefer the later options as its probably easier ;) Regards Ian -- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php