Home  >  Article  >  php教程  >  php ord 函数与中文乱码解决方法

php ord 函数与中文乱码解决方法

WBOY
WBOYOriginal
2016-05-24 08:59:341713browse

php string 函数,定义和用法:ord() 函数返回字符串第一个字符的 ascii 值.

语法:ord(string)

参数 描述

string 必需,要从中获得 ascii 值的字符串.

echo ord('我');这里只能返回230,我是以u8保存的文件并输出的,它得到的只有230, 而230转换成hex是e6,实际上u8中我的编码是e68891,这样你就明白了吧,其实它只拿到了第一个字节.

echo chr(0xe6).chr(0x88).chr(0x91);这里例子可以在u8的情况下输出我这个汉字,你看到了,这里用了3次chr才得到这个汉字,代码如下:

<?php  
	//a字符  
	$str=(pack("a*", "中国"));  
	echo $str,"=",strlen($str),"字节n";  
	getascill($str);  
	//h字符  
	$str=(pack("h*", "fffe"));  
	echo $str,"=",strlen($str),"字节n";  
	getascill($str);  
	//c字符  
	$str=(pack("c*", "55","56","57"));  
	echo $str,"=",strlen($str),"字节n";  
	getascill($str); 
	//i字符 短整形 32位 4个字节 64位8个字节  
	$str=(pack("i", "100"));  
	echo $str,"=",strlen($str),"字节n";  
	getascill($str); 
	//s字符 短整形 2个字节  
	$str=(pack("s", "100"));  
	echo $str,"=",strlen($str),"字节n";  
	getascill($str); 
	//l字符 长整形 4个字节  
	$str=(pack("l", "100"));  
	echo $str,"=",strlen($str),"字节n";  
	getascill($str); 
	//f字符 单精度浮点 4个字节  
	$str=(pack("f", "100"));  
	echo $str,"=",strlen($str),"字节n";  
	getascill($str); 
	//d字符 双精度浮点 8个字节  
	$str=(pack("d", "100"));  
	echo $str,"=",strlen($str),"字节n";  
	getascill($str); 
	function getascill($str)  
	{  
	$arr=str_split($str);  
	foreach ($arr as $v)  
	{  
	echo $v,"=",ord($v),"n";  
	}  
	echo "=============rnrn";  
	} 
	

Statement:
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