In PHP 5.2, json_encode does not covnert non-printable characters ( 0-31 except 8(BACKSPACE), 9(TAB), 10(LF), 12(FF), 13(CR) ) to "\uXXXX" format. json_decdoe can not read them unless it's written in the "\uXXXX" format in any versions of PHP. The following code will fix the problem.
class CJson{
private static function encodeFixForPhp52($str){
$kx=array();
$vx=array();
for($i=0;$i<32;$i++){
if(($i!=8)&&($i!=9)&&($i!=10)&&($i!=12)&&($i!=13)){
$kx[]=chr($i);
$vx[]="\u".str_pad(dechex($i),4,"0",STR_PAD_LEFT);
}
}
return str_replace($kx,$vx,$str);
}
public static function encode($x){
if(defined('JSON_UNESCAPED_UNICODE')){
return json_encode($x,JSON_UNESCAPED_UNICODE);
}else{
return self::encodeFixForPhp52(json_encode($x));
}
}
public static function decode($json){
return json_decode($json,true);
}
}
In PHP v5.2, you may encounter the following error when you use recursion in a RegEx pattern.
unrecognized character after (?
The error messages are like:Warning: preg_replace_callback() [function.preg-replace-callback]:
Compilation failed: unrecognized character after (?
at offset 32 in C:\uroot\www\public_html\testing-center\f-data\test1044\hidy\cmpnt\main\admin\include\CChyJsComp.class.php on line 1
Warning: preg_match_all() [function.preg-match-all]:
Compilation failed: unrecognized character after (?
at offset 99 in C:\uroot\www\public_html\testing-center\f-data\test1044\hidy\cmpnt\main\admin\include\CChyJsComp.class.php on line 1
"unrecognized character after (?" is actually the minus("-") symbol. The cause is the number that specifies the depth of recursion in a regular expression.
/(?[^()]+)|(?-2))*\))/su
/(@[^@{};]+(?:;|(({(?:(?>[^{}"']+|\s+|"(?:\\\\|\\"|\\\n|[^"\n])*?"|'(?:\\\\|\\'|\\\n|[^'\n])*?')|(?-2))*})))|[^@{}
RegEx in PHP v5.2 does not seem to be able to handle the depth of recursion. This issue is more serious than JSON problem, which can be fixed with additional coding.