ord 转换字符串第一个字节为 0-255 之间的值

发表日期:2021-07-01 10:23:22 | 来源: | | 浏览(1543) 分类:字符串 函数

ord

(PHP 4, PHP 5, PHP 7, PHP 8)

ord转换字符串第一个字节为 0-255 之间的值

说明

ord(string $string): int

解析 string 二进制值第一个字节为 0 到 255 范围的无符号整型类型。

如果字符串是 ASCII、 ISO-8859、Windows 1252之类单字节编码,就等于返回该字符在字符集编码表中的位置。 但请注意,本函数不会去检测字符串的编码,尤其是不会识别类似 UTF-8 或 UTF-16 这种多字节字符的 Unicode 代码点(code point)。

该函数是 chr() 的互补函数。

参数

string

一个字符。

返回值

返回 0 - 255 的整型值。

范例

示例 #1 ord() 范例

1<?php
2$str "\n";
3if (ord($str) == 10) {
4    echo "The first character of \$str is a line feed.\n";
5}
6?>

示例 #2 检查 UTF-8 字符串的每一个字节

01<?php
02declare(encoding='UTF-8');
03$str "22";
04for $pos=0;
05 $pos strlen($str);
06 $pos ++ ) {
07 $byte substr($str$pos);
08 echo 'Byte ' $pos ' of $str has value ' . ord($byte) . PHP_EOL;
09}
10?>

以上例程会输出:


Byte 0 of $str has value 240
Byte 1 of $str has value 159
Byte 2 of $str has value 144
Byte 3 of $str has value 152

参见

集速网 copyRight © 2015-2022 宁ICP备15000399号-1 宁公网安备 64010402001209号
与其临渊羡鱼,不如退而结网
欢迎转载、分享、引用、推荐、收藏。