Home > Article > Backend Development > How to convert Chinese to byte in php
php method to convert Chinese into byte: 1. Create a PHP sample file; 2. Convert Chinese into byte array through the "function stringToByteArray($str,$charset) {...}" method. Can.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
php How to convert Chinese to byte?
php Chinese character byte array conversion
<?php function stringToByteArray($str,$charset) { $str = iconv($charset,'UTF-16',$str); preg_match_all('/(.)/s',$str,$bytes); //注:本文的盗版已经有了。不过,提示一下读者,这里的正则改了。 $bytes=array_map('ord',$bytes[1]) ; return $bytes; } function byteArrayToString($bytes,$charset) { $bytes=array_map('chr',$bytes); $str=implode('',$bytes); $str = iconv('UTF-16',$charset,$str); return $str; } $byteArray=stringToByteArray('13亿人口大国,自认为精通PHP的还是相当多的!','utf-8'); print_r($byteArray); $retStr=byteArrayToString($byteArray,'utf-8'); echo $retStr; ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to convert Chinese to byte in php. For more information, please follow other related articles on the PHP Chinese website!