Beispiel #1
0
 /**
  * Convertstring as json_endcode
  */
 function convert_string_non_latin($var)
 {
     require_once JPATH_BASE . DS . 'administrator' . DS . 'components' . DS . 'com_k2' . DS . 'lib' . DS . 'JSON.php';
     $json = new Services_JSON();
     $strlen_var = strlen($var);
     $ascii = '';
     for ($c = 0; $c < $strlen_var; ++$c) {
         $ord_var_c = ord($var[$c]);
         switch (true) {
             case $ord_var_c == 0x8:
                 $ascii .= '\\b';
                 break;
             case $ord_var_c == 0x9:
                 $ascii .= '\\t';
                 break;
             case $ord_var_c == 0xa:
                 $ascii .= '\\n';
                 break;
             case $ord_var_c == 0xc:
                 $ascii .= '\\f';
                 break;
             case $ord_var_c == 0xd:
                 $ascii .= '\\r';
                 break;
             case $ord_var_c == 0x22:
             case $ord_var_c == 0x2f:
             case $ord_var_c == 0x5c:
                 // double quote, slash, slosh
                 $ascii .= '\\' . $var[$c];
                 break;
             case $ord_var_c >= 0x20 && $ord_var_c <= 0x7f:
                 // characters U-00000000 - U-0000007F (same as ASCII)
                 $ascii .= $var[$c];
                 break;
             case ($ord_var_c & 0xe0) == 0xc0:
                 // characters U-00000080 - U-000007FF, mask 110XXXXX
                 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
                 $char = pack('C*', $ord_var_c, ord($var[$c + 1]));
                 $c += 1;
                 $utf16 = $json->utf82utf16($char);
                 $ascii .= sprintf('\\u%04s', bin2hex($utf16));
                 break;
             case ($ord_var_c & 0xf0) == 0xe0:
                 // characters U-00000800 - U-0000FFFF, mask 1110XXXX
                 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
                 $char = pack('C*', $ord_var_c, ord($var[$c + 1]), ord($var[$c + 2]));
                 $c += 2;
                 $utf16 = $json->utf82utf16($char);
                 $ascii .= sprintf('\\u%04s', bin2hex($utf16));
                 break;
             case ($ord_var_c & 0xf8) == 0xf0:
                 // characters U-00010000 - U-001FFFFF, mask 11110XXX
                 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
                 $char = pack('C*', $ord_var_c, ord($var[$c + 1]), ord($var[$c + 2]), ord($var[$c + 3]));
                 $c += 3;
                 $utf16 = $json->utf82utf16($char);
                 $ascii .= sprintf('\\u%04s', bin2hex($utf16));
                 break;
             case ($ord_var_c & 0xfc) == 0xf8:
                 // characters U-00200000 - U-03FFFFFF, mask 111110XX
                 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
                 $char = pack('C*', $ord_var_c, ord($var[$c + 1]), ord($var[$c + 2]), ord($var[$c + 3]), ord($var[$c + 4]));
                 $c += 4;
                 $utf16 = $json->utf82utf16($char);
                 $ascii .= sprintf('\\u%04s', bin2hex($utf16));
                 break;
             case ($ord_var_c & 0xfe) == 0xfc:
                 // characters U-04000000 - U-7FFFFFFF, mask 1111110X
                 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
                 $char = pack('C*', $ord_var_c, ord($var[$c + 1]), ord($var[$c + 2]), ord($var[$c + 3]), ord($var[$c + 4]), ord($var[$c + 5]));
                 $c += 5;
                 $utf16 = $json->utf82utf16($char);
                 $ascii .= sprintf('\\u%04s', bin2hex($utf16));
                 break;
         }
     }
     return $ascii;
 }