errors using preg_replace

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:

http://www.roscripts.com/PHP_regular_expressions_examples-13 6.html


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+& [at] #/%?
=~_|!:,.;]*[-A-Z0-9+& [at] #/%=~_|]','\0', $message);


Here is the error that I'm getting:

Warning: preg_replace(): Delimiter must not be alphanumeric or
backslash in
/var/www/vhosts/cyberbitten.com/httpdocs/sharedCode/
addNewChatMessage.php on
line 42


Can anyone tell me what I'm getting wrong here?
Jake Barnes [ So, 27 Januar 2008 09:45 ] [ ID #1917331 ]

Re: errors using preg_replace

On Sun, 27 Jan 2008 09:45:47 +0100, lawrence k <lkrubner [at] geocities.com> =
=

wrote:

> 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:
>
> http://www.roscripts.com/PHP_regular_expressions_examples-13 6.html
>
>
> Sad to say, I've not been able to get that code to work. Here is what
> I'm doing:
>
> $message =3D addslashes($message);
> $message =3D htmlentities($message);
> $message =3D preg_replace('\b(https?|ftp|file)://[-A-Z0-9+& [at] #/%?
> =3D~_|!:,.;]*[-A-Z0-9+& [at] #/%=3D~_|]','<a href=3D"\0">\0</a>', $message)=
;
>
>
> Here is the error that I'm getting:
>
> Warning: preg_replace(): Delimiter must not be alphanumeric or
> backslash in
> /var/www/vhosts/cyberbitten.com/httpdocs/sharedCode/
> addNewChatMessage.php on
> line 42
>
>
> Can anyone tell me what I'm getting wrong here?

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 =

' [at] ', and if that character occurs in you patters, escape it:

$message =3D =

preg_replace(' [at] \b(https?|ftp|file)://[-A-Z0-9+&\ [at] #/%?=3D~_|! :,.;]*[-A-Z0=
-9+&\ [at] #/%=3D~_|] [at] i','<a =

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

Rik Wasmus
luiheidsgoeroe [ So, 27 Januar 2008 10:11 ] [ ID #1917332 ]
PHP » comp.lang.php » errors using preg_replace

Vorheriges Thema: question about select query in php
Nächstes Thema: Using pop3.class.inc to read email server + retrieve new email +