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
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With