中文字符串取下标转换整数的问题

WBOY
Release: 2016-06-23 14:16:43
Original
1013 people have browsed it

PHP 编码 UTF-8

已知php文件是utf8编码,现有如下内容:
$data = "中";
为什么(int)substr($data, 0, 1) == 0?
如何能转换成正确的整数?

回复讨论(解决方案)

江湖救急,急急急

本帖最后由 xuzuning 于 2013-07-23 18:25:20 编辑

$data = "中";
echo ord(substr($data, 0, 1)); //228

字符串中只有开头的数字符能直接转换为数值
echo '  0123ABC'+0; //123
echo (int)'  0123ABC'; //123

请问如何将substr($data, 0, 1)转换成数字呢

不明白lz到底想干嘛。
取ASCII码然后转int?

引用功能用不了,就是楼上的意思

intval() 或者(int)  既然是ascii码,你需要ord()

intval() 或者(int)  既然是ascii码,你需要ord()
我想循环获取字符串里每个字符的ascii值

是我短路了,可以先取字符串的单个字符,再对每个字符调用ord()

感觉取的还是有问题

$data = "中";$t = unpack('C*', $data);print_r($t);
Copy after login
Array
(
[1] => 228
[2] => 184
[3] => 173
)
$data = "ABCD";$t = unpack('C*', $data);print_r($t);
Copy after login
Array
(
    [1] => 65
    [2] => 66
    [3] => 67
    [4] => 68
)

楼上这个应该可以了

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template