Jump to content

Some help here with a PHP blog posts


apog

Recommended Posts

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

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]);

?>

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.

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...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.