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