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
-
- $str=(pack("a*", "中国"));
- echo $str,"=",strlen($str),"字节n";
- getascill($str);
-
- $str=(pack("h*", "fffe"));
- echo $str,"=",strlen($str),"字节n";
- getascill($str);
-
- $str=(pack("c*", "55","56","57"));
- echo $str,"=",strlen($str),"字节n";
- getascill($str);
-
- $str=(pack("i", "100"));
- echo $str,"=",strlen($str),"字节n";
- getascill($str);
-
- $str=(pack("s", "100"));
- echo $str,"=",strlen($str),"字节n";
- getascill($str);
-
- $str=(pack("l", "100"));
- echo $str,"=",strlen($str),"字节n";
- getascill($str);
-
- $str=(pack("f", "100"));
- echo $str,"=",strlen($str),"字节n";
- getascill($str);
-
- $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";
- }
- ?>
|