Jump to content

RegExp to parse links


lsousa

Recommended Posts

Yes, I know you shouldn't use regexp to parse html but this example could be applied to other things. But anyway, I have this problem:

 

<?
$input = '<html><a href="whatever">nome1</a><a href="whatever&prodID=isso">nome2</a></html>';

$ret = preg_match_all('|href="(.*?prodID=.*?)</a>|',$input,$matches);

var_export($matches);
?>

 

I want it to return only the second link, because that's the one that has prodID as an argument, but even with non-greedy search this is the result:

 

array (
  0 =>
  array (
    0 => 'href="whatever">nome1</a><a href="whatever&prodID=isso">nome2</a>',
  ),
  1 =>
  array (
    0 => 'whatever">nome1</a><a href="whatever&prodID=isso">nome2',
  ),
)

 

 

Shoudln't array[1][0] contain only the second link? How can I do the regexp that only returns the URL of the second link: 'whatever&prodID=isso">nome2' ?

 

 

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/143964-regexp-to-parse-links/
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.