note 110194 deleted from function.preg-last-error by tularis

From: Date: Fri, 28 Sep 2012 18:22:38 +0000
Subject: note 110194 deleted from function.preg-last-error by tularis
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-191068@lists.php.net to get a copy of this message
Note Submitter: falidelu ---- Return values of int preg_last_error(): 0 === PREG_NO_ERROR 1 === PREG_INTERNAL_ERROR 2 === PREG_BACKTRACK_LIMIT_ERROR 3 === PREG_RECURSION_LIMIT_ERROR 4 === PREG_BAD_UTF8_ERROR 5 === PREG_BAD_UTF8_OFFSET_ERROR Trick: PHP manual says constants allow only scalar values... like strings... and PHP manual also says that strings "behave" like (not are) array of characters. <?php // build constant error_code_array as constant string concatenation of "ValueReturnedByFunctionAsErrorCode#WhateverExplanationString##" $str = "0#PREG_NO_ERROR##"; $str .= "1#PREG_INTERNAL_ERROR##"; $str .= "2#PREG_BACKTRACK_LIMIT_ERROR##"; $str .= "3#PREG_RECURSION_LIMIT_ERROR##"; $str .= "4#PREG_BAD_UTF8_ERROR##"; $str .= "5#PREG_BAD_UTF8_OFFSET_ERROR##" ; define( "const_preg_last_error", $str ); // wordwrap... function preg_last_error_string() { return strstr( strstr( const_preg_last_error , preg_last_error()."#" ) , "##", TRUE); // strstr( $arg1, $arg2, TRUE ) requires PHP >= 5.3.0 return mb_strstr( mb_strstr( const_preg_last_error , preg_last_error()."#" ) , "##", TRUE); // mb_strstr() requires PHP >= 5.2.0 $idx_begin = strpos( const_preg_last_error , preg_last_error()."#"); $idx_end = strpos( const_preg_last_error , "##", $idx_begin); return substr( const_preg_last_error , $idx_begin, ($idx_end - $idx_begin) ) ; // strpos and substr are PHP 4 } echo preg_last_error_string() ; ?>

« previous php.notes (#191068) next »