errors using preg_replace

errors using preg_replace

am 27.01.2008 09:45:47 von Jake Barnes

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

Re: errors using preg_replace

am 27.01.2008 10:11:45 von luiheidsgoeroe

On Sun, 27 Jan 2008 09:45:47 +0100, lawrence k =
=

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+&@#/%?
> =3D~_|!:,.;]*[-A-Z0-9+&@#/%=3D~_|]','', $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 '//'. 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 =3D =

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

Rik Wasmus