preg_match_all to retrieve data from HTML page

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LaurentPerche
    New Member
    • Apr 2012
    • 2

    preg_match_all to retrieve data from HTML page

    I am a beginner so bare with me please.

    I am trying to create a PHP script to retrieve the location of an IP address using the info sniper.net webpage.

    I am trying to parse the following HTML code:
    Code:
    map.openInfoWindow(map.getCenter(),"<br /><strong>n/a, Hong Kong</strong>");
    and thought that the following PHP script would work:
    Code:
    <?php
    $str = file_get_contents("http://www.infosniper.net/index.php?ip_address=203.84.219.114");
    $regexp = "#<strong>([a-zA-Z])</strong>#i";
    preg_match_all($regexp,$str,$out, PREG_PATTERN_ORDER);
    echo $out;
    ?>
    It doesn't. what is wrong?
    Last edited by LaurentPerche; Apr 11 '12, 11:20 AM. Reason: couldn't get the [CODE] tag to work
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    1) Backslash is a special character so it has to be escaped.
    2) I have no idea what the # and #i is for. I don't know what you're trying to do with that.
    3) You're only matching one letter between the strong tags. And that letter has to be in the alphabet. It doesn't match at all what you have in your example.

    Comment

    • LaurentPerche
      New Member
      • Apr 2012
      • 2

      #3
      1) There is no Backslash in the regular expression but i guess you are referring to slash / so I changed it to \/
      2) # is like / works the same afaik. I changed it back to \ anyway
      3) I think I got it working now. thanks.
      Code:
      <?php
      $str = file_get_contents("http://www.infosniper.net/index.php?ip_address=203.84.219.114");
      $regexp = "/<strong>(.*)<\/strong>/";
      preg_match_all($regexp, $str, $out, PREG_PATTERN_ORDER);
      echo "\n";
      echo $out[1][0];
      echo "\n";
      ?>

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        I don't know if you want to use the . because if there are multiple strong tags in there, it will only get the most outer pair and then you will have strong tags in your match.

        Comment

        Working...