PHP extract link from <a> tag [duplicate]

為{幸葍}努か 提交于 2019-11-27 20:14:55

This is very easy to do using SimpleXML:

$a = new SimpleXMLElement('<a href="www.something.com">Click here</a>');
echo $a['href']; // will echo www.something.com
Tails

Give this a whirl:

$link = '<a href="www.something.com">Click here</a>';
preg_match_all('/<a[^>]+href=([\'"])(?<href>.+?)\1[^>]*>/i', $link, $result);

if (!empty($result)) {
    # Found a link.
    echo $result['href'][0];
}

Result: www.something.com

Updated: Now requires the quoting style to match, addressing the comment below.

I would suggest following code for this:

$str = '<a href="www.something.com">Click here</a>';
preg_match('/href=(["\'])([^\1]*)\1/i', $str, $m);
echo $m[2] . "\n";

OUTPUT

www.something.com

This will take care of both single quote ' and double quote " in the href link.

Assuming that is ALWAYS the format of the variable, below should do the trick. If the content may not be a link, this won't work. Essentially it looks for data enclosed within two quotations.

<?php

$string = '<a href="www.something.com">Click here</a>';

$pattern = '/"[a-zA-Z0-9.\/\-\?\&]*"/';

preg_match($pattern, $string, $matches);
print_r($matches);
?>

As probably you didn't meant your question that easy, but this does exactly what you're asking for:

$link = '<a href="www.something.com">Click here</a>';
$href = substr($link, 9, -16);

$href is:

string(17) "www.something.com"

As a regular expression it can be expressed it as this is:

$href = preg_match('(^<a href="([^"]*)">Click here</a>$)', $link, $matches) ? $matches[1] : die('Invalid input data.');

Is this helpful?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!