apog Posted April 9, 2008 Share Posted April 9, 2008 Hello everyone! Recently I wrote a PHP blog for my site without using any blog software, so my blog posts are in HTML format. Now I want to add some Adsense announcements right in the the middle of each post, so I figured, what about breaking the text in 2 parts counting any html delimiter like the </p> I use to right the posts, then add the Adsense code and right after that start the second part of the post. So I wrote this: $p_count = substr_count($blog['entry_html'], '</p>'); $p_count_2 = $p_count / 2; $p_count_2--; But now I don't what to do to split the $blog['entry_html'] in two parts right after $p_count_2 times that </p> appears... Anyone have some idea how to do this? or any other idea about how to adding the Adsense code to the post without breaking the HTML structure? Thank you all! Link to comment https://forums.phpfreaks.com/topic/100394-some-help-here-with-a-php-blog-posts/ Share on other sites More sharing options...
Caesar Posted April 9, 2008 Share Posted April 9, 2008 One way I suppose could be... <?php $html = "<p>first paragraph</p>\n"; $html .= "<p>second paragraph</p>"; $html_array = explode("\n",$html); $adsens = "<script>.........</script>"; $html_array[1] = str_replace('</p>', '</p>'.$adsens, $html_array[1]); ?> Link to comment https://forums.phpfreaks.com/topic/100394-some-help-here-with-a-php-blog-posts/#findComment-513425 Share on other sites More sharing options...
apog Posted April 9, 2008 Author Share Posted April 9, 2008 Thank you, but that can't help me. Each post has many <p></p> and I need the one of the middle in order to separate the post in 2 parts and ad the adsense code right in the middle of the post without breaking the HTML structure. Cheers! Link to comment https://forums.phpfreaks.com/topic/100394-some-help-here-with-a-php-blog-posts/#findComment-513434 Share on other sites More sharing options...
gluck Posted April 9, 2008 Share Posted April 9, 2008 Not sure if I understood your problem but you can count the length of the blog string and use substring to select the part where you want to insert the adsense. Now you might want to check for the paragraph <P> which is closest to the middle point so that you can insert you content in it. Link to comment https://forums.phpfreaks.com/topic/100394-some-help-here-with-a-php-blog-posts/#findComment-513458 Share on other sites More sharing options...
apog Posted April 9, 2008 Author Share Posted April 9, 2008 Thank you Gluck, that is exactly what I need to do. BUT HOW? How can I split the post in the middle but right after the "center" </p>? Link to comment https://forums.phpfreaks.com/topic/100394-some-help-here-with-a-php-blog-posts/#findComment-513462 Share on other sites More sharing options...
apog Posted April 10, 2008 Author Share Posted April 10, 2008 Well, I solved it. $post = explode('</p>', $blog['entry_html']); $p_count = count($post); $p_count = ($p_count / 2) + 1; $i = 0; foreach ($post as $k) { if ($i == $p_count) { echo 'Adsense_code'; } echo $k . '</p>'; $i++; } But there should be a better way to do this, since I am taking the </p> as the reference to cut the HTML post, where I should be cutting the post right in the middle without breaking the HTML structure... but well... Link to comment https://forums.phpfreaks.com/topic/100394-some-help-here-with-a-php-blog-posts/#findComment-513508 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.