You are here: Re: errors using preg_replace « PHP Programming Language « IT news, forums, messages
Re: errors using preg_replace

Posted by Rik Wasmus on 09/29/05 12:01

On Sun, 27 Jan 2008 09:45:47 +0100, lawrence k <lkrubner@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-136.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~_|]','<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 =

'@', 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','<a =

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

Rik Wasmus

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация