Get the App
SLTechnology News&Howtos  ›  Development  › 

How to convert unicode into Chinese characters in php

Shulou Source: shulou.com Published: 2022-06-03 03:50:15 09月23日 Update

This article mainly introduces the php unicode how to convert Chinese characters, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor with you to understand.

Function unicode_encode ($name) {$name = iconv ('UTF-8',' UCS-2', $name); $len = strlen ($name); $str =''; for ($I = 0; $I

< $len - 1; $i = $i + 2){$c = $name[$i];$c2 = $name[$i + 1];if (ord($c) >

0) {/ / two-byte text $str. ='\ u'.base_convert (ord ($c), 10,16). Base_convert (ord ($c2), 10,16);} else {$str. = $c2;}} return $str } / / Decoding the UNICODE encoded content function unicode_decode ($name) {/ / Transcoding, converting the Unicode encoding to browsable utf-8 encoding $pattern ='/ ([\ w] {4})) / if (! empty ($matches)) {$name ='; for ($j = 0; $j < count ($matches [0])) $str = $matches [0] [$j]; if (strpos ($str,'\ u') = = 0) {$code = base_convert (substr ($str, 2,2), 16,10); $code2 = base_convert (substr ($str, 4), 16,10); $c = chr ($code). Chr ($code2); $c = iconv ('UCS-2',' UTF-8', $c); $name. = $c;} else {$name. = $str;}} return $name;}

Above are two functions of php, one converts Chinese characters into unicode and the other converts unicode into Chinese characters.

JSON.stringify (data) at work; this method converts Chinese characters into unicode, but it fails to parse unicode into Chinese characters in php, so the study found that the original unicode format has changed, the correct unicode format is:\ u5730\ u65b9, Chinese characters mean 'place'. But during php reverse parsing, the unicode becomes u5730u65b9, without the slash. So it can be parsed by changing the format in such a case, like this: $name=unicode_decode (str_replace ('Upright Personality\ UPU65b9')); so it can be parsed correctly.

Thank you for reading this article carefully. I hope the article "how to convert unicode into Chinese characters in php" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Chinese characters articles coding format is in two lieutenant general value interest content function at the same time place byte situation meaning text slash method time Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Microsoft MySQL Redmi Shulou Tech Info