errors using preg_replace

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I wanted to take some input and change it so that all the plain text
URLs got wrapped in HTML hyperlinks. I found a bit of preg_replace on
this site, which I thought I could use:

Sad to say, I've not been able to get that code to work. Here is what
I'm doing:

    $message = addslashes($message);
    $message = htmlentities($message);
    $message = preg_replace('\b(https?|ftp|file)://[-A-Z0-9+&@#/%?
=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|]','<a href=""></a>', $message);

Here is the error that I'm getting:

Warning: preg_replace(): Delimiter must not be alphanumeric or
backslash in
addNewChatMessage.php on
line 42

Can anyone tell me what I'm getting wrong here?

Re: errors using preg_replace



Quoted text here. Click to load it
Quoted text here. Click to load it

A delimiter is the starting character of a regex, end after the second  =

occurance of those, all characters are considered modifiers (/s for sing=
le  =

line matching, /i for case insensitivity, /x for whitespace & comments  =

etc.). The most used character as a delimiter is '/', so a typical regex=

would be '/<patter>/<modifiers>'. In this case, the engine expects you t=
o  =

want \ as a delimiter, as it's the first character (from \b), but this  =

isn't allowed as it is an escaping character. You can use almost any  =

character you like as a delimiter, and in HTML context the '/' as  =

delimiter is not the best choice, as it would require you to escape all =
/   =

 from closing tags (\/). You could use some more 'abnormal character', l=
ike  =

'@', and if that character occurs in you patters, escape it:

$message =  =

-9+&\@#/%=3D~_|]@i','<a  =

href=3D"$0">$0</a>', $message);
-- =

Rik Wasmus

Site Timeline