views:

119

answers:

3

Hi all,

I've got the following regular expression that works fine on my testing server, but just returns an empty string on my hosted server.

$text = preg_replace('~[^\\pL\d]+~u', $use, $text);

Now I'm pretty sure this comes down to the hosting server version of PCRE not being compiled with Unicode property support enabled. The differences in the two versions are as follows:

My server:

PCRE version 7.8 2008-09-05
Compiled with
UTF-8 support
Unicode properties support
Newline sequence is LF
\R matches all Unicode newlines
Internal link size = 2
POSIX malloc threshold = 10
Default match limit = 10000000
Default recursion depth limit = 10000000
Match recursion uses stack

Hosting server:

PCRE version 4.5 01-December-2003
Compiled with
UTF-8 support
Newline character is LF
Internal link size = 2
POSIX malloc threshold = 10
Default match limit = 10000000
Match recursion uses stack

Also note that the version on the hosting server (the same version PHP is compiled against) is pretty old.

What confuses me though, is that pcretest fails on both servers from the command line with

re> ~[^\\pL\d]+~u
** Unknown option 'u'

although this regexp works fine when run from PHP on my server.

So, I guess my questions are does the regular expression fail on the hosting server because of the lack of Unicode properties? Or is there something else that I'm missing?

Thanks all, Gaz.

+1  A: 

Most likely, the PCRE on the host server doesn't have UTF8 support enabled.

pcretest uses different options. It's '8' for UTF-8, instead of 'u'. That's the reason of the error.

Please also notice that different PCRE libraries can be used in Apache or CLI versions of PHP on the same machine.

ZZ Coder
A: 

To answer your questions; yes, and no. I believe PCRE 5.0 was the threshold when that UTF8 support became available.

salathe
+1  A: 

PCRE 4.5 does support UTF-8 but does not support Unicode properties such as \pL and does not support the /u flag that enables them. You'll need to upgrade PCRE on your server to version 5.0 or later. You may also need to upgrade PHP to have support for /u in preg_replace().

Jan Goyvaerts