Forum Moderators: coopster
$pattern = "/<a\s+href=['\"]([^'\"]+)['\"][^>]*>/i";
$matches = array();
if(preg_match_all($pattern, $string, $matches)) {
echo '<pre>'; print_r($matches); echo '</pre>';
} else {
echo 'Cannot find any matches in the string.';
}
The pattern can actually get a bit more complex than that since, for example, one could put singles quotes within double quotes and vice versa, but that should be a good place to start.
For additional information regarding regular expressions check out the syntax: [us3.php.net...]
[edited by: eelixduppy at 4:48 pm (utc) on April 13, 2009]
$pattern = "/<a\s+href=['\"]([^'\"]+)['\"][^>]*>/i";
I would do this a little differently to account fo unquoted and malformed code
< a href = somelink.html>
or inclusion of other attributes and their possible rearrangement.
<a class="something" href="somelink.html" title="oops">
<a title="oops" class="something" href='somelink.html'>
$pattern = "/<.*href\s*=\s*['\"]*([^'\">]+)['\"]*>/i";
< - starts with
.* - followed by zero or more of any character
href followed by the important part, "href"
\s*=\s* - followed by =, with zero or more spaces on either side
['\"]* - followed by zero or more quotes
( - Start saving match in $1
[^'\">]+ - followed by any character NOT a ',", or > (> is needed in this part if the link is unquoted)
) - end saving match in $1
['\"]* - followed by zero or more quotes - without this, if they exist, the quotes will get slurped in $1
> - end of pattern
Needs to be tested, but you can see the approach might be more forgiving of malformed or unexpected code.