views:

49

answers:

2

i use preg_match_all and need to grab all a href="" tags in my code, but i not relly understand how to its work.

i have this reg. exp. ( /(<([\w]+)[^>]>)(.?)(<\/\2>)/ ) its take all html codes, i need only all a href tags.

i hobe i can get help :)

A: 
<?
$html = '<a href="http://something.com" target="_blank">Test </a>';
if (preg_match('/href="([^"]*)"/i', $html , $regs))
{
   $result = $regs[1];
} else {
   $result = "No URL Found";
}
echo $result ;
?>
sea_1987
firstly, <a> tags arent the only tags that can have href attributes (the <link> tag also has them).secondly, this will also match href='soemthing' outside of < and >, in which case its not actually a tag.
Mailslut
Tanks but its aslo take ".css" urls and more... :) i need only <a href="(myurl)">(title)</a>
NeoNmaN
pass whatever you want to $html it does preg_match on $html
sea_1987
+1  A: 

I'm not a fan of parsing HTML with RegExp, but anyway:

$input_string = file_get_contents(
    "http://stackoverflow.com/questions/2817449/preg-match-all-problems/2817549"
);

preg_match_all(
    '@\\<a\\b[^\\>]+\\bhref\\s*=\\s*"([^"]*)"[^\\>]*\\>@i',
    $input_string,
    $matches
);

var_dump( $matches ); // inspect for useful information

It expects that all hrefs are enclosed inside ". Won't work otherwise.

Salman A