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

?>

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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