NYCPHP Meetup

NYPHP.org

[nycphp-talk] PCRE, utf8, Exceptions and you

Mark Armendariz lists at enobrev.com
Sat Apr 14 17:36:27 EDT 2007


Afternoon all,

What I'm trying to do...
------------------------
The validation classes in my library use PCRE's utf8 modifier.
http://us2.php.net/manual/en/reference.pcre.pattern.modifiers.php (look for
PCRE_UTF8)

This allows me to do something like this:
echo preg_replace('/[^\p{L}\p{Nd}\p{Zs}]/u', '', 'abc123@#$'); // outputs
abc123

Which for ASCII is the equivalent of:
echo preg_replace('/[^\d\w\s]/', '', 'abc123@#$'); // outputs abc123 - or
echo preg_replace('/[^a-zA-Z0-9 ]/', '', 'abc123@#$'); // outputs abc123

Or rather, replaces anything that's not a letter, number or space, but with
the added benefit of supporting Unicode characters.
 

The Problem
-----------
Unfortunately, some (definitely not all) of my clients' on managed hosting
servers don't have PCRE's Unicode support configured.  I'm dealing with
their support staff to resolve the issue when possible, but would like to
look for these issues in my library as well to degrade and handle them
gracefully.

It seems php does not allow you to catch the PCRE warning given by the
server if PCRE isn't compiled to support Unicode.  I've seen this in the
past with named captures ('/(?P<numbers>[\d]+)/'), and resorted to just
taking out the named parts instead of working around the error.


Proposed Solution
-----------------
In this case, I would like to be able to fall back to ASCII checks if the
support isn't built in, like this:
try {
	echo preg_replace('/[^\p{L}\p{Nd}\p{Zs}]/u', '', $sVar);
}
catch (Exception $e) {
      echo preg_replace('/[^\d\w\s]/', '', $sVar);
}

And then probably output to a log warning me (and not my beloved visitors)
that the server needs to be reconfigured)


The Error
---------
Unfortunately this doesn't throw the exception.  It just outputs:

"Warning: preg_replace() [function.preg-replace]: Compilation failed: PCRE
does not support \L, \l, \N, \P, \p, \U, \u, or \X at offset 3 in
/home/whatever/pcretest.php on line 3"

I suppose I could capture the error with an error handler and try to figure
out if it was the error I'm looking for, but that seems so messy and
potentially unreliable.  Aren't we supposed to be able to use exceptions for
this sort of thing in php5+?


The Question
------------
Does anyone have experience with catching PCRE errors or just preg function
errors in general and dealing with them accordingly?


Thank You
---------
Mark Armendariz




More information about the talk mailing list