SoFunction
Updated on 2025-03-04

Example of method of PHP to implement Unicode encoding mutual conversion

This article describes the method of PHP to convert Unicode encoding into each other. Share it for your reference, as follows:

<?php
/**
 * $str Original Chinese string
 * $encoding The encoding of the original string, default utf-8
 * $prefix encoded prefix, default "&#"
 * $postfix encoded suffix, default ";"
 */
function unicode_encode($str, $encoding = 'utf-8', $prefix = '&#', $postfix = ';') {
 //Split the string $str = iconv("UTF-8", "gb2312", $str);
 $cind = 0;
 $arr_cont = array();
 for ($i = 0; $i < strlen($str); $i++) {
 if (strlen(substr($str, $cind, 1)) > 0) {
  if (ord(substr($str, $cind, 1)) < 0xA1) { //If it is English, take 1 byte  array_push($arr_cont, substr($str, $cind, 1));
  $cind++;
  } else {
  array_push($arr_cont, substr($str, $cind, 2));
  $cind+=2;
  }
 }
 }
 foreach ($arr_cont as &$row) {
 $row = iconv("gb2312", "UTF-8", $row);
 }
 //Convert Unicode code foreach ($arr_cont as $key => $value) {
 $unicodestr.= $prefix . base_convert(bin2hex(iconv('utf-8', 'UCS-4', $value)), 16, 10) .$postfix;
 }
 return $unicodestr;
}
/**
 * $str Unicode encoded string
 * $decoding The encoding of the original string, default utf-8
 * $prefix The prefix of the encoded string, default "&#"
 * $postfix encoded string suffix, default ";"
 */
function unicode_decode($unistr, $encoding = 'utf-8', $prefix = '&#', $postfix = ';') {
 $arruni = explode($prefix, $unistr);
 $unistr = '';
 for ($i = 1, $len = count($arruni); $i < $len; $i++) {
 if (strlen($postfix) > 0) {
  $arruni[$i] = substr($arruni[$i], 0, strlen($arruni[$i]) - strlen($postfix));
 }
 $temp = intval($arruni[$i]);
 $unistr .= ($temp < 256) ? chr(0) . chr($temp) : chr($temp / 256) . chr($temp % 256);
 }
 return iconv('UCS-2', $encoding, $unistr);
}
$str = "PHP Programming:";
$unistr = unicode_encode($str);
$unistr2 = unicode_decode($unistr);
echo $unistr . '<br />';
echo $unistr2 . '<br />';
$unistr = unicode_encode($str,'GBK','\\u');
$unistr2 = unicode_decode($unistr,'GBK','\\u');
echo $unistr . '<br />';
echo $unistr2 . '<br />';

PS: The following tests this function is more useful, and the code needs to be run in the utf-8 encoding environment

function unicode_encode($name) {//Unicode encoding $jsonarr = array($name);
 $jsonstr = json_encode($jsonarr);
 if (empty ($jsonstr))
 return '';
 return substr($jsonstr,2,-2);
}
function unicode_decode($name) {//Unicode decoding
 $json = '{"str":"' . $name . '"}';
 $arr = json_decode($json, true);
 if (empty ($arr))
 return '';
 return $arr['str'];

}

$test = "\u811a\u672c\u4e4b\u5bb6";
echo "unicode decoding:".unicode_decode($test)."<br/>";
echo "unicode encoding:".unicode_encode('I')."<br/>";

PS: Here are a few Unicode encoding and conversion operations related tools for your reference:

OnlineUnicode/Chinese conversion tool:
http://tools./transcoding/unicode_chinese

Native/UnicodeOnline encoding conversion tool:
http://tools./transcoding/native2unicode

Online Chinese characters/ASCII code/UnicodeCoding mutual conversion tool:
http://tools./transcoding/chinese2unicode

For more information about PHP related content, please check out the topic of this site:Summary of PHP encoding and transcoding operation techniques》、《PHP object-oriented programming tutorial》、《Summary of PHP mathematical operation skills》、《Complete collection of PHP array (Array) operation techniques》、《Summary of usage of php strings》、《PHP data structure and algorithm tutorial》、《Summary of PHP Programming Algorithm》、《Summary of usage of php regular expressions",and"Summary of common database operation techniques for php

I hope this article will be helpful to everyone's PHP programming.