Reputation: 441
I can;t figure out what's wrong with my line:
$ret = preg_replace( "(http://|https://|http://www.|https://www.)([[:alnum:]#?~/&=._-]+)", "<a href=\"\\1\\2\" target=\"_blank\">\\1\\2</a>", $ret);
I tried inserting a / in the first pos of the first arg but that didn't work.
I'm stumped. Thanks for any help.
Upvotes: 0
Views: 321
Reputation: 91488
Just add delimiter to your regex, choose one that is not present in your expression to avoid escaping, here I suggest !
:
$ret = preg_replace( "!(http://|https://|http://www.|https://www.)([[:alnum:]#?~/&=._-]+)!", "<a href=\"$1$2\" target=\"_blank\">$1$2</a>", $ret);
You could also simplify a bit:
$ret = preg_replace( "!(https?://(?:www\.)?[\w#?~/&=.-]+)!", "<a href=\"$1\" target=\"_blank\">$1</a>", $ret);
Upvotes: 2
Reputation: 2536
$ret = preg_replace(
'/((?:http:\/\/|https:\/\/)(?:www\.)?)([[:alnum:]#?~\/&=._-]+)/',
'<a href="$1$2" target="_blank">$1$2</a>',
$ret
);
It should work like this. Basically, you need to escape the forward slashes in your regex.
Upvotes: 0