Например: И => u0418 А => u0410 Какой функцией можно такое сделать?
2 answers
In Unicode, "And" will be "1048", and "A" - "1040", where did you get these codes from?
Here is the working conversion function (taken from php.net):
function uniord($ch) { $n = ord($ch{0}); if ($n < 128) { return $n; // no conversion required } if ($n < 192 || $n > 253) { return false; // bad first byte || out of range } $arr = array(1 => 192, // byte position => range from 2 => 224, 3 => 240, 4 => 248, 5 => 252, ); foreach ($arr as $key => $val) { if ($n >= $val) { // add byte to the 'char' array $char[] = ord($ch{$key}) - 128; $range = $val; } else { break; // save some e-trees } } $retval = ($n - $range) * pow(64, sizeof($char)); foreach ($char as $key => $val) { $pow = sizeof($char) - ($key + 1); // invert key $retval += $val * pow(64, $pow); // dark magic } return $retval; }
- The author indicated the codes in the 16-tric system - yozh
- Very strange code. For the characters Ё, Ё, yu, and I in cp-1251, return false . Decimal codes CP1251 A: 192 ... e: 253 S: 254 I: 255 S: 184 Y: 168 - avp
- so it is 1251, but you need Unicode - DemoS
- As I understand it, the parameter of the function uniord ($ ch) is a character in cp1251, and its result is a character in Unicode. Is not it ? - avp
- Well, so what, returns in Unicode, that's right. And what have your codes in cp1251? - DemoS
|
<?php function jdecoder($json_str) { $cyr_chars = array ( '\u0430' => 'а', '\u0410' => 'А', '\u0431' => 'б', '\u0411' => 'Б', '\u0432' => 'в', '\u0412' => 'В', '\u0433' => 'г', '\u0413' => 'Г', '\u0434' => 'д', '\u0414' => 'Д', '\u0435' => 'е', '\u0415' => 'Е', '\u0451' => 'ё', '\u0401' => 'Ё', '\u0436' => 'ж', '\u0416' => 'Ж', '\u0437' => 'з', '\u0417' => 'З', '\u0438' => 'и', '\u0418' => 'И', '\u0439' => 'й', '\u0419' => 'Й', '\u043a' => 'к', '\u041a' => 'К', '\u043b' => 'л', '\u041b' => 'Л', '\u043c' => 'м', '\u041c' => 'М', '\u043d' => 'н', '\u041d' => 'Н', '\u043e' => 'о', '\u041e' => 'О', '\u043f' => 'п', '\u041f' => 'П', '\u0440' => 'р', '\u0420' => 'Р', '\u0441' => 'с', '\u0421' => 'С', '\u0442' => 'т', '\u0422' => 'Т', '\u0443' => 'у', '\u0423' => 'У', '\u0444' => 'ф', '\u0424' => 'Ф', '\u0445' => 'х', '\u0425' => 'Х', '\u0446' => 'ц', '\u0426' => 'Ц', '\u0447' => 'ч', '\u0427' => 'Ч', '\u0448' => 'ш', '\u0428' => 'Ш', '\u0449' => 'щ', '\u0429' => 'Щ', '\u044a' => 'ъ', '\u042a' => 'Ъ', '\u044b' => 'ы', '\u042b' => 'Ы', '\u044c' => 'ь', '\u042c' => 'Ь', '\u044d' => 'э', '\u042d' => 'Э', '\u044e' => 'ю', '\u042e' => 'Ю', '\u044f' => 'я', '\u042f' => 'Я', '\r' => '', '\n' => '<br />', '\t' => '' ); foreach ($cyr_chars as $key => $value) { $json_str = str_replace($key, $value, $json_str); } return $json_str; } echo jdecoder("\u0421\u043a\u0430\u0447\u0430\u0442\u044c"); ?>
Encoder (just rearranged two variables :)
<?php function jencoder($json_str) { $cyr_chars = array ( '\u0430' => 'а', '\u0410' => 'А', '\u0431' => 'б', '\u0411' => 'Б', '\u0432' => 'в', '\u0412' => 'В', '\u0433' => 'г', '\u0413' => 'Г', '\u0434' => 'д', '\u0414' => 'Д', '\u0435' => 'е', '\u0415' => 'Е', '\u0451' => 'ё', '\u0401' => 'Ё', '\u0436' => 'ж', '\u0416' => 'Ж', '\u0437' => 'з', '\u0417' => 'З', '\u0438' => 'и', '\u0418' => 'И', '\u0439' => 'й', '\u0419' => 'Й', '\u043a' => 'к', '\u041a' => 'К', '\u043b' => 'л', '\u041b' => 'Л', '\u043c' => 'м', '\u041c' => 'М', '\u043d' => 'н', '\u041d' => 'Н', '\u043e' => 'о', '\u041e' => 'О', '\u043f' => 'п', '\u041f' => 'П', '\u0440' => 'р', '\u0420' => 'Р', '\u0441' => 'с', '\u0421' => 'С', '\u0442' => 'т', '\u0422' => 'Т', '\u0443' => 'у', '\u0423' => 'У', '\u0444' => 'ф', '\u0424' => 'Ф', '\u0445' => 'х', '\u0425' => 'Х', '\u0446' => 'ц', '\u0426' => 'Ц', '\u0447' => 'ч', '\u0427' => 'Ч', '\u0448' => 'ш', '\u0428' => 'Ш', '\u0449' => 'щ', '\u0429' => 'Щ', '\u044a' => 'ъ', '\u042a' => 'Ъ', '\u044b' => 'ы', '\u042b' => 'Ы', '\u044c' => 'ь', '\u042c' => 'Ь', '\u044d' => 'э', '\u042d' => 'Э', '\u044e' => 'ю', '\u042e' => 'Ю', '\u044f' => 'я', '\u042f' => 'Я', '\r' => '', '\n' => '<br />', '\t' => '' ); foreach ($cyr_chars as $key => $value) { $json_str = str_replace($value, $key, $json_str); } return $json_str; } echo jencoder("Скачать"); ?>
|