Page 1 of 1

Need helps in PHP coding - to insert ads in middle of blog p

Posted: Tue Jun 01, 2010 10:02 am
by hyjlmc
Hi,
I'm new here and I don't know whether should I ask my question here or in newbies section, so, let it be here.

I found this code in google search, for inserting ads in the middle of the blog post.

Code: Select all

function inject_ad_text_after_n_chars($content) { 
  // only do this if post is longer than 2000 characters 
  $enable_length = 2000; 
  // insert after the first </p> after 1500 characters 
  $after_character = 1500; 
  if (is_single() && strlen($content) > $enable_length) { 
    $before_content = substr($content, 0, $after_character); 
    $after_content = substr($content, $after_character); 
    $after_content = explode('</p>', $after_content); 
    $text = ' 
Your Ads Here 
    '; 
    array_splice($after_content, 1, 0, $text); 
    $after_content = implode('</p>', $after_content); 
    return $before_content . $after_content; 
  } 
  else { 
    return $content; 
  } 
} 
add_filter('the_content', 'inject_ad_text_after_n_chars'); 
This code works well with image and text post.

The problem I'm encounter is, when I do a post which contain <table></table>, like comparison chart I commonly use <table> code. The ads will be inserted into the table and make the table very weird, because of a 768x90 ads present inside the table column.

I tried to modified the code to

Code: Select all

function inject_ad_text_after_n_chars($content) { 
  // only do this if post is longer than 2000 characters 
  $enable_length = 1800; 
  // insert after the first </p> after 2200 characters 
  $after_character = 1550; 
  if (is_single() && strlen($content) > $enable_length) { 
    $before_content = substr($content, 0, $after_character); 
    $after_content = substr($content, $after_character); 
    $after_content = explode('</p>', $after_content); 
    $text = ' 
Your Ads Here 
    '; 
    array_splice($after_content, 1, 0, $text); 
    $after_content = implode('</p>', $after_content); 
    return $before_content . $after_content; 
  } 
  elseif (is_single() && strlen($content) > $enable_length) { 
    $before_content = substr($content, 0, $after_character); 
    $after_content = substr($content, $after_character); 
    $after_content = explode('</table>', $after_content); 
    $text = ' 
Your Ads Here 
    '; 
    array_splice($after_content, 1, 0, $text); 
    $after_content = implode('</table>', $after_content); 
    return $before_content . $after_content; 
  } 
  else { 
    return $content; 
  } 
} 
add_filter('the_content', 'inject_ad_text_after_n_chars'); 
And this time, the ads works well with blog post contain <table>, but in the images and text post, the ads will present on the bottom of post, so it mixed with the bottom ads.

What I want to do is:
1. In images and text post, it will works as original, insert the ads just after the first </p> after certain character.
2. In post with <table>, it will insert the ads before the table if the character meet the 1. situation, else insert the ads into bottom of the table, but not inside the <table> body.

I'm appreciate your helps.

Thank you.

Re: Need helps in PHP coding - to insert ads in middle of bl

Posted: Tue Jun 01, 2010 7:00 pm
by Christopher
You probably want to start over with the original code, but use preg_split() instead of explode() to find either '</p>' or '</table>'. Then you can place the add after whichever comes first.

Re: Need helps in PHP coding - to insert ads in middle of bl

Posted: Wed Jun 02, 2010 12:17 am
by hyjlmc
Hi,
Thanks for reply.
Actually I don't know how to write PHP script, can you help me to write the code?
Is it the code like this?

Code: Select all

function inject_ad_text_after_n_chars($content) { 
  // only do this if post is longer than 2000 characters 
  $enable_length = 1800; 
  // insert after the first </p> after 2200 characters 
  $after_character = 1550; 
  if (is_single() && strlen($content) > $enable_length) { 
    $before_content = substr($content, 0, $after_character); 
    $after_content = substr($content, $after_character); 
    $after_content = preg_split('</p>', $after_content); 
    $text = ' 
Your Ads Here 
    '; 
    array_splice($after_content, 1, 0, $text); 
    $after_content = implode('</p>', $after_content); 
    return $before_content . $after_content; 
  } 
  elseif (is_single() && strlen($content) > $enable_length) { 
    $before_content = substr($content, 0, $after_character); 
    $after_content = substr($content, $after_character); 
    $after_content = preg_split('</table>', $after_content); 
    $text = ' 
Your Ads Here 
    '; 
    array_splice($after_content, 1, 0, $text); 
    $after_content = implode('</table>', $after_content); 
    return $before_content . $after_content; 
  } 
  else { 
    return $content; 
  } 
} 
add_filter('the_content', 'inject_ad_text_after_n_chars');