Selecting link inside href PHP
How do I select http://test.com/
out of this HTML:
<a target="_new" href="http://test.com/" title="test">$293.00</a>
I realize I should use a DOM parser, however its for a few lines of code and I'd rather just use regex.
I've tried this with n开发者_StackOverflow中文版o success:
preg_match('/(http|https)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/', $html, $url);
Thanks.
You can solve this with lookarounds:
if (preg_match('/(?<=href=").*?(?=")/', $html, $groups)) {
$url = $groups[0];
} else {
$url = "";
}
(?<=href=")
means: find a position immediately afterhref="
- Then match anything, but as little as possible with
.*?
-- this is saved ingroups[0]
- Stop, when you found a position where the next character is
"
When I don't use a DOM parser, it means that I am pretty sure about the contents, and end up with something REALLY simple... like this...
preg_match('/href="([^"]+)"/', $html, $regs);
url will be in $regs[1].
If you just want the href, all you have to do is preg_match('/href="([^"]*?)/', $html, $matches)
and then grab the content of $matches[1]
.
Well, certainly your parser doesn't match tons of legal URLs. More usefully, see http://mathiasbynens.be/demo/url-regex
Note more specifically, /\S* matches /" which is not what you want, presumably.
精彩评论