I have a string that looks like:
$string = '<a href="http://google.com">http://google.com</a>';
How can I remove the http://
part from the link text, but leave it in the href attribute?
I have a string that looks like:
$string = '<a href="http://google.com">http://google.com</a>';
How can I remove the http://
part from the link text, but leave it in the href attribute?
Without using a full blown parser, this may do the trick for most situations...
$str = '<a href="http://google.com">http://google.com</a>';
$regex = '/(?<!href=["\'])http:\/\//';
$str = preg_replace($regex, '', $str);
var_dump($str); // string(42) "<a href="http://google.com">google.com</a>"
It uses a negative lookbehind to make sure there is no href="
or href='
preceding it.
It also takes into account people who delimit their attribute values with '
.