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

Discussion in 'PHP' started by hyjlmc, Jun 1, 2010.

  1. #1
    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.
    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'); 
    PHP:
    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
    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'); 
    PHP:
    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.
     
    hyjlmc, Jun 1, 2010 IP
  2. mehmetm

    mehmetm Well-Known Member

    Messages:
    134
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #2
    your condition in the code is illogical. you should use a condition like that
    
    if(preg_match('<table>',$content)) {
    ...
    } else {
    ....
    }
    
    PHP:
    I'm not so good string funtions and regex in php. However I guess what you want is so easy for a pro coder..
     
    mehmetm, Jun 1, 2010 IP
  3. Trikun3

    Trikun3 Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Can you give an example of what you want the source to be, and the source for what's happening that you're not wanting?
     
    Trikun3, Jun 1, 2010 IP
  4. hyjlmc

    hyjlmc Peon

    Messages:
    363
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi mehmetm, Can you write the complete code here?

    Hi Trikun3, I want the ads (like adsense) appear on the middle of a single post (wordpress blog). from the code above, only in the post length (character) is more than 1800 will insert the ads on the first </p> after 1550 character.
    However, in some posts I have put the <table> and the ads is inserting into the first </p> after 1550 character but within the </table>, so the post will look like this:
    So what I want it, if the </p> code is present within the </table>, then the ads should be inserted after the first </table>, but not </p> in this case.
     
    hyjlmc, Jun 1, 2010 IP
  5. mehmetm

    mehmetm Well-Known Member

    Messages:
    134
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #5
    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):
      
      
    		if(preg_match('<table>',$content)):
    		
    	
    			$pos = strpos($content,'</table>')+8;
    	
    			$before_content = trim(substr($content, 0, $pos)); 
    			$after_content = trim(substr($content, $pos)); 
    			$text = 'Your Ads Here'; 
    			$after_content = $text.$after_content;
    					
    			return $before_content . $after_content; 
    			
    		
    		else: 
    		
    		
    			$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; 		
    			
    			
    		endif;
    
    	else:
    	
    		return $content;
    	
    	endif;
    	
    } 
    
    add_filter('the_content', 'inject_ad_text_after_n_chars');
    PHP:
    that should work for you..
     
    mehmetm, Jun 2, 2010 IP
    hyjlmc likes this.
  6. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #6
    Yeah it will work!
    Like wise if you have flash then put as <embedded> instead of table
     
    roopajyothi, Jun 3, 2010 IP
  7. hyjlmc

    hyjlmc Peon

    Messages:
    363
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    thanks mehmetm, it works excellently, rep given!
     
    hyjlmc, Jun 4, 2010 IP
  8. mehmetm

    mehmetm Well-Known Member

    Messages:
    134
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #8
    nice to hear that :) your welcome :)
     
    mehmetm, Jun 4, 2010 IP