[nycphp-talk] PCRE, utf8, Exceptions and you
Mark Armendariz
lists at enobrev.com
Mon Apr 16 13:35:34 EDT 2007
> On 4/14/07, Mark Armendariz <lists at enobrev.com> wrote:
>
> > 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+?
>
> 5.2 introduced recoverable errors, I believe those can be caught...
> doc is spotty on this though.
>
I'd just noticed it right after I made the post. I plan on mixing it in and
I'm glad to see them, though the client server I'm currently working with is
on 5.16 at the moment, so I still have to work around it for now.
> A warning is just a warning, so can't be caught, though it
> can be supressed.
No doubt. I try to catch every little error, warning and notice I get and
handle or report it accordingly when possible so I can have a production
server running E_STRICT without a bead of sweat. Anal to be sure, but it
leaves me warm and fuzzy and makes for happy portability.
>
> If you haven't already solved this problem, I would suggest a
> test where you try compiling a very simple preg_match using
> the unicode codes, suppress any warnings with @, and see if
> you get the expected result. If you do, proceed with unicode,
> if not log and fall back to ascii.
>
No doubt a good suggestion. I ended up trapping the error with my error
handler class doing a strpos for 'preg_', which is doing the job well.
Here's a couple excerpts (slightly modified so they'll make sense here):
class PregException extends Exception {
public function __construct($sMessage, $iCode = 0, $sFile, $iLine,
$aContext = null) {
parent::__construct($sMessage, $iCode);
$this->file = $sFile;
$this->line = $iLine;
$this->context = $aContext;
}
}
class Error {
function __construct() {
set_error_handler(array(&$this, 'eh'));
}
function eh($iErrorNumber, $sError, $sFile, $iLine, $aContext) {
if (strpos($sError, 'preg_') !== false) {
throw new PregException($sError, $iErrorNumber, $sFile, $iLine,
$aContext);
} else {
$this->add($iErrorNumber, $sError, $sFile, $iLine, $aContext);
}
return true;
}
// other error handler code
}
// part of my validation class
function checkRegex($mValue) {
// Unicode Letters, Digits and Spaces
try {
$bBadData = preg_match('/[^\p{L}\p{Nd}\p{Zs}]/u', $mValue);
}
catch (PregException $e) {
$bBadData = preg_match('/[^\w\s]/', $mValue);
}
if ($bBadData) {
return false;
}
return true;
}
Thanks for the suggestions!
Mark
More information about the talk
mailing list