By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,991 Members | 1,911 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,991 IT Pros & Developers. It's quick & easy.

preg_match_all to retrieve data from HTML page

P: 2
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 webpage.

I am trying to parse the following HTML code:
Expand|Select|Wrap|Line Numbers
  1. map.openInfoWindow(map.getCenter(),"<br /><strong>n/a, Hong Kong</strong>");
and thought that the following PHP script would work:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $str = file_get_contents("");
  3. $regexp = "#<strong>([a-zA-Z])</strong>#i";
  4. preg_match_all($regexp,$str,$out, PREG_PATTERN_ORDER);
  5. echo $out;
  6. ?>
It doesn't. what is wrong?
Apr 11 '12 #1
Share this Question
Share on Google+
3 Replies

Expert Mod 10K+
P: 12,349
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.
Apr 11 '12 #2

P: 2
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.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $str = file_get_contents("");
  3. $regexp = "/<strong>(.*)<\/strong>/";
  4. preg_match_all($regexp, $str, $out, PREG_PATTERN_ORDER);
  5. echo "\n";
  6. echo $out[1][0];
  7. echo "\n";
  8. ?>
Apr 12 '12 #3

Expert Mod 10K+
P: 12,349
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.
Apr 12 '12 #4

Post your reply

Sign in to post your reply or Sign up for a free account.