PHP character to Unicode encoding function

Source: Internet
Author: User

/** * $str the original String * $encoding the encoding of the original string, the default GBK * $prefix the encoded prefix, the default "*" * $postfix the encoded suffix, the default ";" * *functionUnicode_encode($str, $encoding = ' GBK ', $prefix = ' the ', $postfix = '; ') {    $str = Iconv($encoding, ' UCS-2 ', $str);    $arrstr = Str_split($str, 2);    $unistr = "';     for($i = 0, $len = Count($arrstr); $i < $len; $i++) {        $dec = Hexdec(Bin2Hex($arrstr[$i]));        $unistr .= $prefix . $dec . $postfix;    }     return $unistr;}?/** * $STR Unicode encoded String * $encoding the encoding of the original string, the default GBK * $prefix encoded string prefix, the default "* * * * $postfix the suffix of the encoded string, default"; "* *functionUnicode_decode($unistr, $encoding = ' GBK ', $prefix = ' the ', $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 <  the)?CHR(0) . CHR($temp) : CHR($temp /  the) . CHR($temp %  the);    }     return Iconv(' UCS-2 ', $encoding, $unistr);}

Use:
//GBK String Test$str = ' <b> haha </b> ';Echo $str.' <br/> ';?$unistr =Unicode_encode($str);Echo $unistr.' <br/> '; //& #60;& #98;& #62;& #21704;& #21704;& #60;& #47;& #98;& #62;?$str 2 =Unicode_decode($unistr);Echo $str 2.' <br/> '; //<b> haha </b>?//utf-8 String Test$utf 8_str = Iconv(' GBK ', ' UTF-8 ', $str);Echo $utf 8_str.' <br/> '; //<b> å 堝 搱 </b> Note: UTF shows the garbled characters in GBK! Switchable browser-coded test?$utf 8_unistr =Unicode_encode($utf 8_str, ' UTF-8 ');Echo $utf 8_unistr.' <br/> '; //& #60;& #98;& #62;& #21704;& #21704;& #60;& #47;& #98;& #62;?$utf 8_str2 =Unicode_decode($utf 8_unistr, ' UTF-8 ');Echo $utf 8_str2.' <br/> '; //<b> å 堝 搱 </b>?//Other suffix, prefix test$prefix _unistr =Unicode_encode($str, ' GBK ', "\ \U", "');Echo $prefix _unistr.' <br/> '; //\u60\u98\u62\u21704\u21704\u60\u47\u98\u62?$profix _unistr2 =Unicode_decode($prefix _unistr, ' GBK ', "\ \U", "');Echo $profix _unistr2.' <br/> '; //<b> haha </b>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.