Jump to content

Broken Preg_match


fullahimhard

Recommended Posts

Hiyas,

 

I am trying to pregmatch the telephone number

 

the html code is:

<tr class='attribute'><td class='attribute-label' valign='top'>Telephone: </td><td class='attribute-value'>9556856</td></tr>

 

My preg_match uses:

preg_match('|valign=\'top\'>Telephone: </td><td class=\'attribute-value\'>(.*?)</td></tr>|im', $result1, $match3); 

 

But it isnt picking anything up. Any ideas why?

 

Cheers :)

Link to comment
https://forums.phpfreaks.com/topic/160789-broken-preg_match/
Share on other sites

My first choice would probably be DOM / XPath (there's always other ways to skin a cat), but if going the regex route, one method could be:

 

Example:

$str = "<tr class='attribute'><td class='attribute-label' valign='top'>Telephone: </td><td class='attribute-value'>9556856</td></tr>";

preg_match('#Telephone: </td><td[^>]+class=[\'"]attribute-value[\'"]>(\d+)</td>#i', $str, $match);
echo $match[1]; // Output: 9556856

 

Granted, the (\d+) assumes that there are only consecutive digits for the value of that td tag. If you want to just match anything to make sure, that part of the pattern could be replaced with ([^<]+) instead

Link to comment
https://forums.phpfreaks.com/topic/160789-broken-preg_match/#findComment-848648
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.