Exemplo n.º 1
0
 public static function encode($input)
 {
     $output = "";
     $chr1 = $chr2 = $chr3 = $enc1 = $enc2 = $enc3 = $enc4 = "";
     $i = 0;
     $input = self::utf8_encode($input);
     while ($i < mb_strlen($input)) {
         $chr1 = Z_Unicode::charCodeAt($input, $i++);
         $chr2 = Z_Unicode::charCodeAt($input, $i++);
         $chr3 = Z_Unicode::charCodeAt($input, $i++);
         $enc1 = $chr1 >> 2;
         $enc2 = ($chr1 & 3) << 4 | $chr2 >> 4;
         $enc3 = ($chr2 & 15) << 2 | $chr3 >> 6;
         $enc4 = $chr3 & 63;
         if (is_nan($chr2)) {
             $enc3 = $enc4 = 64;
         } else {
             if (is_nan($chr3)) {
                 $enc4 = 64;
             }
         }
         $output = $output . Z_Unicode::charAt(self::$keyStr, $enc1) . Z_Unicode::charAt(self::$keyStr, $enc2) . Z_Unicode::charAt(self::$keyStr, $enc3) . Z_Unicode::charAt(self::$keyStr, $enc4);
     }
     return $output;
 }
Exemplo n.º 2
0
 public static function convertCharStr2CP($textString, $preserve, $pad, $type)
 {
     // converts a string of characters to code points, separated by space
     // textString: string, the string to convert
     // preserve: string enum [ascii, latin1], a set of characters to not convert
     // pad: boolean, if true, hex numbers lower than 1000 are padded with zeros
     // type: string enum[hex, dec, unicode, zerox], whether output should be in hex or dec or unicode U+ form
     $haut = 0;
     $n = 0;
     $CPString = '';
     $afterEscape = false;
     for ($i = 0; $i < mb_strlen($textString); $i++) {
         $b = Z_Unicode::charCodeAt($textString, $i);
         if ($b < 0 || $b > 0xffff) {
             throw new Exception('Error in convertChar2CP: byte out of range ' . dechex($b) . '!');
         }
         if ($haut != 0) {
             if (0xdc00 <= $b && $b <= 0xdfff) {
                 //alert('12345'.slice(-1).match(/[A-Fa-f0-9]/)+'<');
                 //if ($CPString.slice(-1).match(/[A-Za-z0-9]/) != null) { $CPString += ' '; }
                 if ($afterEscape) {
                     $CPString .= ' ';
                 }
                 if (type == 'hex') {
                     $CPString .= dechex(0x10000 . ($haut - 0xd800 << 10) . ($b - 0xdc00));
                 } else {
                     if (type == 'unicode') {
                         $CPString .= 'U+' + dechex(0x10000 . ($haut - 0xd800 << 10) . ($b - 0xdc00));
                     } else {
                         if (type == 'zerox') {
                             $CPString .= '0x' + dechex(0x10000 . ($haut - 0xd800 << 10) . ($b - 0xdc00));
                         } else {
                             $CPString .= 0x10000 . ($haut - 0xd800 << 10) . ($b - 0xdc00);
                         }
                     }
                 }
                 $haut = 0;
                 continue;
             } else {
                 throw new Exception('Error in convertChar2CP: surrogate out of range ' . dechex($haut) . '!');
             }
         }
         if (0xd800 <= $b && $b <= 0xdbff) {
             $haut = $b;
         } else {
             if ($b <= 127 && $preserve == 'ascii') {
                 $CPString .= Z_Unicode::charAt($textString, $i);
                 $afterEscape = false;
             } else {
                 if ($b <= 255 && $preserve == 'latin1') {
                     $CPString .= Z_Unicode::charAt($textString, $i);
                     $afterEscape = false;
                 } else {
                     //if ($CPString.slice(-1).match(/[A-Za-z0-9]/) != null) { $CPString += ' '; }
                     if ($afterEscape) {
                         $CPString .= ' ';
                     }
                     if ($type == 'hex') {
                         $cp = dechex($b);
                         if ($pad) {
                             while (strlen($cp) < 4) {
                                 $cp = '0' . $cp;
                             }
                         }
                     } else {
                         if ($type == 'unicode') {
                             $cp = dechex($b);
                             if ($pad) {
                                 while (strlen($length) < 4) {
                                     $cp = '0' . $cp;
                                 }
                             }
                             $CPString .= 'U+';
                         } else {
                             if ($type == 'zerox') {
                                 $cp = dechex($b);
                                 if ($pad) {
                                     while (strlen($cp) < 4) {
                                         $cp = '0' . $cp;
                                     }
                                 }
                                 $CPString .= '0x';
                             } else {
                                 $cp = $b;
                             }
                         }
                     }
                     $CPString .= $cp;
                     $afterEscape = true;
                 }
             }
         }
     }
     return strtoupper($CPString);
 }