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 :)
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 :)
<?
$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 ;
?>
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.