views:

408

answers:

2

Hello all,

I have got stuck with a question I have just been helped on - its a new problem but only just slightly.

I have this preg_match to get the contents of href. Please don't tell me not to use regex - I am aware of using other parsers/classes etc but this is an old script that just needs to be fixed for now. :) No time for re-writes!

preg_match("~<a target=\'_blank\' rel=\'nofollow\' href=\"(.*?)\">~i", $epilink, $epiurl);

It returns:

http://www.example.com/frame2.php?view=&amp;epi=54673-r

However, it should return:

http://www.example.com/frame2.php?view=168204&amp;epi=54673

This is an example of html it would work with:

<a target='_blank' rel='nofollow' href="http://www.example.com/frame2.php?view=545903&amp;epi=54683"&gt;

Why is the URL I have returned malformed?

Thanks all for any help.

+1  A: 
$string="<a target='_blank' rel='nofollow' href=\"http://www.example.com/frame2.php?view=545903&amp;epi=54683\"&gt;";
$s = explode('">',$string);
foreach($s as $k){
   if (strpos($k,"href")!==FALSE){
        echo preg_replace('/.*href="|/ms',"",$k);
        break;
   }
}

output

$ php test.php
http://www.example.com/frame2.php?view=545903&amp;epi=54683
ghostdog74
The string will contain more html. As I have said to Anon how would I just get one match? Btw, thanks for replying to my previous question.
Abs
first match? you can exit the for loop. see edit
ghostdog74
A: 
minnur