PHP extract link from <a> tag

61,340

Solution 1

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

Solution 2

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.

Solution 3

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.

Solution 4

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);
?>
Share:
61,340
5et
Author by

5et

Updated on June 23, 2020

Comments

  • 5et
    5et almost 4 years

    Possible Duplicate:
    PHP String Manipulation: Extract hrefs

    I am using php and have string with content =

    <a href="www.something.com">Click here</a>

    I need to get rid of everything except "www.something.com" I assume this can be done with regular expressions. Any help is appreciated! Thank you