Home > php教程 > php手册 > body text

php怎么把unicode编码转为utf-8编码

WBOY
Release: 2016-05-26 08:20:09
Original
2237 people have browsed it

编码转换一直是一个比较头痛的问题我们通常会因一些编码问题导致页面乱码了,下文小编整理了一个unicode编码转为utf-8编码的例子,大家一起来看看.

在前端开发中,为了让中文在不同的环境下都能很好的显示,一般是将中文转化为unicode格式,即u4f60,比如:"你好啊"的unicode编码为"u4f60u597du554a".

JS里将中文转为unicode编码很简单,JS代码:

function convert2Unicode(str) { 
	return str.replace(/[u0080-uffff]/g, 
	function($0) { 
		var tmp = $0.charCodeAt(0).toString(16); 
		return "u" + new Array(5 - tmp.length).join('0') + tmp; 
	}); 
}
Copy after login

反转也很简单,直接alert出来或者innerHTML到dom节点里都可以,但如果将u4f60u597du554a"字符传递给php,php就不能直接echo或者其他操作了,直接echo的话还是原生的字符,不能自动转化为中文.

php将unicode转为utf-8方法

在 php5.0及以上版本中提供了json_encode,json_decode方法,在使用json_encode变量的时候,如果变量里含有中文的话,会将中文转为unicode格式,所以在想是否可以通过 json_decode将unicode转为中文呢?实际测试发现是可以的,但对单一的字符串发现有些问题.

对于简单的字符串,发现有时候使用json_decode转的化,结果直接为空了,但将字符串替换为数组然后在转就可以了,下面为封装的代码:

function unicode2utf8($str){ 
	        if(!$str) return $str; 
	        $decode = json_decode($str); 
	        if($decode) return $decode; 
	        $str = '["' . $str . '"]'; 
	        $decode = json_decode($str); 
	        if(count($decode) == 1){ 
	                return $decode[0]; 
	        } 
	        return $str; 
	}
Copy after login

使用这个方法可以很好的将unicode编码转为utf-8编码,附上js转为实体字符和php将实体字符转为汉字的方法.

js将汉字转为实体字符:

function unicode2utf8($str){ 
	if(!$str) return $str; 
	$decode = json_decode($str); 
	if($decode) return $decode; 
	$str = '["' . $str . '"]'; 
	$decode = json_decode($str); 
	if(count($decode) == 1){ 
			return $decode[0]; 
	} 
	return $str; 
}
Copy after login

php将实体字符转为utf-8汉字的方法:

function entity2utf8onechar($unicode_c){ 
	$unicode_c_val = intval($unicode_c); 
	$f=0x80; // 10000000 
	$str = ""; 
	// U-00000000 - U-0000007F:   0xxxxxxx 
	if($unicode_c_val <= 0x7F){         $str = chr($unicode_c_val);     }     //U-00000080 - U-000007FF:  110xxxxx 10xxxxxx     else if($unicode_c_val >= 0x80 && $unicode_c_val <= 0x7FF){         $h=0xC0; // 11000000         $c1 = $unicode_c_val >> 6 | $h; 
		$c2 = ($unicode_c_val & 0x3F) | $f; 
		$str = chr($c1).chr($c2); 
	} 
	//U-00000800 - U-0000FFFF:  1110xxxx 10xxxxxx 10xxxxxx 
	else if($unicode_c_val >= 0x800 && $unicode_c_val <= 0xFFFF){         $h=0xE0; // 11100000         $c1 = $unicode_c_val >> 12 | $h; 
		$c2 = (($unicode_c_val & 0xFC0) >> 6) | $f; 
		$c3 = ($unicode_c_val & 0x3F) | $f; 
		$str=chr($c1).chr($c2).chr($c3); 
	} 
	//U-00010000 - U-001FFFFF:  11110xxx 10xxxxxx 10xxxxxx 10xxxxxx 
	else if($unicode_c_val >= 0x10000 && $unicode_c_val <= 0x1FFFFF){         $h=0xF0; // 11110000         $c1 = $unicode_c_val >> 18 | $h; 
		$c2 = (($unicode_c_val & 0x3F000) >>12) | $f; 
		$c3 = (($unicode_c_val & 0xFC0) >>6) | $f; 
		$c4 = ($unicode_c_val & 0x3F) | $f; 
		$str = chr($c1).chr($c2).chr($c3).chr($c4); 
	} 
	//U-00200000 - U-03FFFFFF:  111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 
	else if($unicode_c_val >= 0x200000 && $unicode_c_val <= 0x3FFFFFF){         $h=0xF8; // 11111000         $c1 = $unicode_c_val >> 24 | $h; 
		$c2 = (($unicode_c_val & 0xFC0000)>>18) | $f; 
		$c3 = (($unicode_c_val & 0x3F000) >>12) | $f; 
		$c4 = (($unicode_c_val & 0xFC0) >>6) | $f; 
		$c5 = ($unicode_c_val & 0x3F) | $f; 
		$str = chr($c1).chr($c2).chr($c3).chr($c4).chr($c5); 
	} 
	//U-04000000 - U-7FFFFFFF:  1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 
	else if($unicode_c_val >= 0x4000000 && $unicode_c_val <= 0x7FFFFFFF){         $h=0xFC; // 11111100         $c1 = $unicode_c_val >> 30 | $h; 
		$c2 = (($unicode_c_val & 0x3F000000)>>24) | $f; 
		$c3 = (($unicode_c_val & 0xFC0000)>>18) | $f; 
		$c4 = (($unicode_c_val & 0x3F000) >>12) | $f; 
		$c5 = (($unicode_c_val & 0xFC0) >>6) | $f; 
		$c6 = ($unicode_c_val & 0x3F) | $f; 
		$str = chr($c1).chr($c2).chr($c3).chr($c4).chr($c5).chr($c6); 
	} 
	return $str; 
} 
function entities2utf8($unicode_c){ 
	$unicode_c = preg_replace("/&#([da-f]{5});/es", "entity2utf8onechar(&#39;\1&#39;)", $unicode_c); 
	return $unicode_c; 
}
Copy after login

使用方式:$utf8chars = entities2utf8("啊你好啊");


文章地址:

转载随意^^请带上本文地址!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template