content of a webpage from snippets of another

Discussion in 'PHP' started by uca, Jan 2, 2007.

  1. #1
    Let's say that I have a webpage with a lot of text.
    Let's say that I would like to randomly pick a paragraph and add it (with a rotator) to another page within the same website or to another one in a box.

    Assuming that what I said above is clear enough to understand, doea anybody have any suggestions?

    Thanks for any help!

    :)
     
    uca, Jan 2, 2007 IP
  2. trevlar

    trevlar Peon

    Messages:
    65
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I'll give it a go.

    This is untested and assumes you have a valid markup with opening and closing paragraph tags around your paragraphs.

    
    // grab the source code
    $filesource = file_get_contents("http://www.example.com/filename.html");
    
    // split it at all the opening paragraph tags
    $paragraphs = explode("<p>", $filesource);
    
    // only go on if there are indeed any paragraphs on that page
    if (count($paragraphs) > 0) {
    
    // get a random number between 1 and the total number of paragraphs.
    $rand_num = rand(1, count($paragraphs));
    
    // let's work with a random paragraph from that page
    $paragraph = $paragraphs[$rand_num];
    
    // now split it at the closing paragraph tags
    $paragraph = explode("</p>", $paragraph);
    
    // print the result
    echo $paragraph[0];
    
    }
    
    
    PHP:
    You should only have to replace http://www.example.com/filename.html
     
    trevlar, Jan 2, 2007 IP
    uca likes this.
  3. uca

    uca Well-Known Member

    Messages:
    2,242
    Likes Received:
    69
    Best Answers:
    0
    Trophy Points:
    155
    #3
    Wow trevlar!

    A little more advanced a solution than I expected but it'll give me an opportunity to learn.

    Thanks very much and don't be surprised if in the next few days I show up with some questions...

    Thanks! Greenies on their way!

    :)
     
    uca, Jan 2, 2007 IP