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
Share on other sites

Picks it up for me.  How are you displaying the matches?

 

print_r($match3);

 

Also, if you put your pattern in double quotes you won't need to escape all the inner single quotes.

Link to comment
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.