Conversion of Chinese characters to PHP: Unicode (UTF8) to GBK

Source: Internet
Author: User
Tags chr php download
P> eyes without hate GBK Unicode UTF8 Kanji Conversion
The conversion of Chinese characters in PHP has always been a tricky business.
The class has four filters "&#[dec];", "& #x [hex];", "%u[hex]", "UTF8 conversion"
Easy to use, but also can customize the filter to do their favorite operation
qswhu.php Download from here
/user/qswh/qswhu.zip
Class qswhu{
var $qswhData;
function Qswhu ($filename = "qswhu.php") {
$this->qswhdata=file ($filename);
}

function decode ($STR, $pattern =0) {
$arr =array ("/&# (w+);/iu", "/((%WW) +)/I", "/%u (w{4,5})/iu");
if (Is_integer ($pattern)) {
if ($pattern >=count ($arr)) Die ("Invalid Function");
$pattern = $arr [$pattern];
}
Return Preg_replace_callback ($pattern, Array ($this, "U2GB"), $STR);
}

function U2GB ($arr) {
/****** (Qiushuiwuhen 2002-8-15) ******/
$ret = ""; $str = $arr [1];
if (Preg_match_all ("/%w{2}/", $str, $matches)) {
For ($i =0 $i <count ($matches [0]); $i + +) {
$CHR 1=hexdec (substr ($matches [0][$i],1));
$arr =array ("F0", "E0", "C0", "0");
For ($j =0 $j <count ($arr); $j + +) if ($chr 1>hexdec ($arr [$j])) break;
$CHR =hexdec (substr ($matches [0][$i],1))-hexdec ($arr [$j]);
while (+ + $j <count ($arr)) $CHR = $chr *0x40+ (Hexdec (substr ($matches [0][++ $i],1)) -0x80);
$str =dechex ($CHR);
if (strlen ($STR) ==4) {
$p =hexdec (substr ($str, 0,2)) -0x4d;
$q =hexdec (substr ($STR, 2)) *4;

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.