Reading data off a web page help.

Discussion in 'PHP' started by aaron_nimocks, Oct 9, 2006.

  1. #1
    I have been searching and searching and I cant seem to find any code on what I am trying to do.

    Basically all I want is a function that will look at a web page and tell me if a phrase is present or not. I think I would have to use a regular expresion but that stuff just confuses me.

    For example

    Function

    If phrase "google" is on www.google.com

    else

    Just something that simple. I know it can be done in just a few lines of code but I am clueless.

    Any help?
     
    aaron_nimocks, Oct 9, 2006 IP
  2. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #2
    1. Grab the content with file_get_contents
    2. If regExp confuses you and you just need to know whether it's on there or not, just use if (strpos("google", $fetched_data)) { echo 'hooray!'; }
     
    T0PS3O, Oct 9, 2006 IP
  3. aaron_nimocks

    aaron_nimocks Im kind of a big deal Staff

    Messages:
    5,563
    Likes Received:
    627
    Best Answers:
    0
    Trophy Points:
    420
    #3
    Sounds good.

    But I forgot to mention I am learning PHP and I need stuff spelled out more. :)

    I know you love to help people so if can take 3.5 minutes and write the code I will declare today a holiday in your name.
     
    aaron_nimocks, Oct 9, 2006 IP
  4. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #4
    LOL... You're lucky then... (Quick before Mad4 beats me)

    
    $url_to_grab = "http://www.digitalpoint.com";
    $fetched_data = file_get_contents($url_to_grab); //http://uk2.php.net/file_get_contents
    $looking_for = "Shawn";
    if (strpos($fetched_data, $looking_for)) { // http://uk2.php.net/manual/en/function.strpos.php
    echo 'Shawn is found on ' . $url_to_grab;
    } else {
    echo 'No go! Try again!';
    }
    PHP:
     
    T0PS3O, Oct 9, 2006 IP
    aaron_nimocks likes this.
  5. aaron_nimocks

    aaron_nimocks Im kind of a big deal Staff

    Messages:
    5,563
    Likes Received:
    627
    Best Answers:
    0
    Trophy Points:
    420
    #5
    Tested and works like a charm.

    Congratulations and I am sorry christopher columbus but you now have to share your holiday with T0PS3O!
     
    aaron_nimocks, Oct 9, 2006 IP
  6. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Woohoo I officially rule now!

    (Smart ass comes in now and posts better code :D )
     
    T0PS3O, Oct 9, 2006 IP
  7. klown

    klown Peon

    Messages:
    2,093
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hard to beat that tops! Good ez to understand code
     
    klown, Oct 9, 2006 IP
  8. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thanks klown :) I've already been thinking about improvements. We can honour robots.txt and if we crawl multiple pages of the same domain, we will slow down by using sleep(); Caching can be implemented and we can use Curl as well. We can add reporting, stats and graphs on performance.

    G:eek:
     
    T0PS3O, Oct 9, 2006 IP
  9. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #9
    I would use eregi instead of strpos, this was capitalization won't matter.

    Peace,
     
    Barti1987, Oct 9, 2006 IP
  10. aaron_nimocks

    aaron_nimocks Im kind of a big deal Staff

    Messages:
    5,563
    Likes Received:
    627
    Best Answers:
    0
    Trophy Points:
    420
    #10
    I would agree also, but I just need to search for exact phrase so capitlization wont be an issue.
     
    aaron_nimocks, Oct 9, 2006 IP
  11. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #11
    I might call strip_tags on the fetched data before searching for the string so that you won't search for the string among HTML comments/code. Also strpos will return 0 if the string is found at the very beginning of the fetched data, and returns boolean false if it's not found. You can do

    if (strpos ($feched_data, $string) !== false) {
    ...
    }

    :)
     
    exam, Oct 9, 2006 IP