Welcome Guest, Not a member yet? Register   Sign In
Trying to use error_get_last
#1

In a portion of my script, I try to validate a regex pattern provided by a user, not testing it against a string, but just executing it arbitrarily, then checking for PHP errors. I tested it in a stand alone script and it works perfectly fine, however when I throw the code into a CI library method, it doesnt work at all. I googled it and I found some people saying they had error_get_last() working fine in CI, until they upgraded to a recent version.

Is error_get_last disabled for some reason? is there another way to accomplish the exact same thing?

Heres the stand alone script I was talking about:
PHP Code:
<?php

$pattern 
'/890(*D)(F*asdf/asdf98SDF$#%@#$';
$str '';

$regex = @preg_match($pattern$str);

$error error_get_last();

if(isset(
$error['message']))
    echo 
"Regex Error: {$error['message']}PHP_EOL;
elseif(
$regex)
    echo 
"Matched" PHP_EOL;
else
    echo 
"No Match" PHP_EOL

Again, that works perfectly fine, until thrown into CI

Thanks!
Reply
#2

As mentioned in the manual notes for error_get_last(), there are many reasons that it may not work, including the error handler set by CodeIgniter returning a non-FALSE value. Since I haven't tested this, I don't know if that's a strict check; CI has a void return from the error handler if the severity of the error doesn't meet the error reporting level. CI 2 returned from the error handler under slightly different circumstances, and the error reporting levels changed slightly between versions, too.

The manual notes contain a number of potential work-arounds, too. In this case, though, I wonder if preg_last_error() has the same issues, or if it just wouldn't work for your situation.
Reply
#3

(07-17-2015, 07:42 AM)mwhitney Wrote: As mentioned in the manual notes for error_get_last(), there are many reasons that it may not work, including the error handler set by CodeIgniter returning a non-FALSE value. Since I haven't tested this, I don't know if that's a strict check; CI has a void return from the error handler if the severity of the error doesn't meet the error reporting level. CI 2 returned from the error handler under slightly different circumstances, and the error reporting levels changed slightly between versions, too.

The manual notes contain a number of potential work-arounds, too. In this case, though, I wonder if preg_last_error() has the same issues, or if it just wouldn't work for your situation.

Good idea, I just gave it a shot tho..

Code:
PHP Code:
$pattern '/asdf/df/asdf/adsf';
$str '';

$preg_errs = array(
 
   PREG_NO_ERROR               => 'No Error',
 
   PREG_INTERNAL_ERROR         => 'Internal Error',
 
   PREG_BACKTRACK_LIMIT_ERROR  => 'Backgrack Limit Error',
 
   PREG_RECURSION_LIMIT_ERROR  => 'Recursion Limit Error',
 
   PREG_BAD_UTF8_ERROR         => 'Bad UTF8 Error',
 
   PREG_BAD_UTF8_OFFSET_ERROR  => 'Bad UTF8 Offset Error'
);

preg_match($pattern$str);

$err preg_last_error();

echo 
"ERROR: {$preg_errs[$err]}\n"

Output: 
Quote:PHP Warning:  preg_match(): Unknown modifier 'd' in /Users/jhyland/PhpstormProjects/SASSET-App/htdocs/preg.php on line 14


Warning: preg_match(): Unknown modifier 'd' in /Users/jhyland/PhpstormProjects/SASSET-App/htdocs/preg.php on line 14
ERROR: No Error

Odd, thats not even via CI  Dodgy
Reply




Theme © iAndrew 2016 - Forum software by © MyBB