SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru
    Join Date
    Sep 2004
    Location
    Provo, UT
    Posts
    844
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Inserting Google Adsense in the middle of article text

    My site has pages that are dynamically created from the database. Once Iíve pulled the text out of the database I do the following:

    $testimonialText = stripslashes($testimonialText);
    $testimonialText = ereg_replace(10,"<br />",$testimonialText);
    $testimonialText = ereg_replace(34,"'",$testimonialText);

    What would be the best way to implement the following line in the middle of the text? I want readers to see a google ad right in the middle of the article. Iíve seen this type of thing on many pages, but not sure of the best way to tackle it.

    $testimonialText .= file_get_contents ('adsense/banner.php');


    Thank you.
    Convert your dollars into silver coins. www.convert2silver.com

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You could split the article on sentences ( . ). Output n sentences, output your banner, output the rest.
    PHP Code:
    $test "Sentence 1. Sentence 2. Sentence 3. Sentence 4.";

    $sentences explode(". "$test);

    $banner "I'm only here for the beer. ";

    // line of dbg, rm this
    var_dump($sentences);

    $first array_slice($sentences,,2);
    $last array_slice($sentences2);

    $output join(". ",$first) . '. ';
    $output .= $banner;
    $output .= join(". "$last);
    echo 
    $output;

    //gives:
    // some debug, see whats going on
    array
      
    => string 'Sentence 1' (length=10)
      
    => string 'Sentence 2' (length=10)
      
    => string 'Sentence 3' (length=10)
      
    => string 'Sentence 4.' (length=11)

    // your sentences with an advert in them
    Sentence 1. Sentence 2. I'm only here for the beer. Sentence 3. Sentence 4. 

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Edit: Actually that can be simplified, as array_splice actually removes the first instances from the original array ....
    PHP Code:
    $test "Sentence 1. Sentence 2. Sentence 3. Sentence 4.";

    $sentences explode(". "$test);

    $banner "I'm only here for the beer. ";

    $first array_splice($sentences,,2);

    // var_dump($sentences);  // now only contains remaining elements

    $output join(". ",$first) . '. ' $banner ;
    $output .= join(". "$sentences);
    echo 
    $output
    You will run into trouble with this if your text from the db contains other instances of ". " (dot space), or more importantly if your text contains html markup.

    You may find you are splitting on sentences within tags, such as:
    PHP Code:
    $test "Sentence 1. <div>Sentence 2. Sentence 3.</div> Sentence 4."
    Is that indeed your case?

  4. #4
    SitePoint Guru
    Join Date
    Sep 2004
    Location
    Provo, UT
    Posts
    844
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Cups, thanks for your help. I took what you suggested, made some minor changes and tried to learn something from this. For some reason, however, I’m getting two periods after the end of each sentence. Here is my code:


    $testimonialText = nl2br($testimonialText); // This will insert <br /> tags into the string

    // Split on whitespace between sentences preceded by a punctuation mark
    $sentences = preg_split('/(?<=[.?!;:])\s+/', $testimonialText, -1, PREG_SPLIT_NO_EMPTY);

    $banner = "<p><b>Google ads go here</b></p>";


    $first = array_splice($sentences,0 ,14);


    $output = join(". ",$first) . '. ' . $banner ;
    $output .= join(". ", $sentences);

    print ("<p>$output</p>");

    echo '<pre>';
    print_r($sentences);
    echo '</pre>';





    Maybe this would work better if instead of counting the number of sentences, it counts the number of paragraphs? Once the number of paragraphs are known, we could divide that number by two and then insert the Google ads right in the middle. Thoughts? Here are the results if you want to see them:

    oil-testimonials DOT com/testSentences.php?tID=1430

    Thanks!
    Convert your dollars into silver coins. www.convert2silver.com

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Split on paragraphs if that makes more sense to you.

    You have to be very clear in your mind that you have catered for every single possibility of what constitutes a "paragraph" when the output is coming from a textarea (or worse, a wysiwyg-enabled textarea).

    Set up some test cases in an array and keep working on the solution till you have what you want and it works 100%.

    PHP Code:

    $tests 
    = array(
      
    "<p>Para 1</p><p>Para 2</p>"  // what you might expect
    "<p>Para 1<br /><br /><br />Para 2</p>"  // should this replace 2 <br />s with a <p>?
    "Para1<br /><br /></p>" // how should this be handled?
    );
    // add more esoteric examples if you want your function to be bombproof

    array_map($test"MySplittingFunction"); 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •