RalphF
RalphF

Reputation: 441

Warning: preg_replace() [function.preg-replace]: Unknown modifier

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

Answers (2)

Toto
Toto

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

Jonan
Jonan

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

Related Questions