ord

(PHP 4, PHP 5, PHP 7)

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 的整型值。

范例

Example #1 ord() 范例

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

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

<?php
declare(encoding='UTF-8');
$str "🐘";
for ( 
$pos=0$pos strlen($str); $pos ++ ) {
 
$byte substr($str$pos);
 echo 
'Byte ' $pos ' of $str has value ' ord($byte) . PHP_EOL;
}
?>

以上例程会输出:


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

参见

相关文章
php math 函数 在任意进制之间转换数字php 字符串 函数 返回使用 htmlspecialchars 和 htmlentities 后的转换表php 字符串 函数 将逻辑顺序希伯来文(logical hebrew)转换为视觉顺序希伯来文(visual hebrew),并且转换换行符php 字符串 函数 转换十六进制字符串为二进制字符串php 多字节字符串 函数 对字符串进行大小写转换php 多字节字符串 函数 转换字符的编码php 多字节字符串 函数 转换一个或多个变量的字符编码php 多字节字符串 函数 为 mime 头编码字符串php 多字节字符串 函数 encode character to html numeric string referencephp 多字节字符串 函数 get aliases of a known encoding typephp 多字节字符串 函数 获取 mbstring 的内部设置php 多字节字符串 函数 检测 http 输入字符编码php 多字节字符串 函数 设置/获取 http 输出字符编码php 多字节字符串 函数 在输出缓冲中转换字符编码的回调函数php 多字节字符串 函数 获取按指定宽度截断的字符串php 多字节字符串 函数 大小写不敏感地查找字符串在另一个字符串中首次出现的位置php 多字节字符串 函数 大小写不敏感地查找字符串在另一个字符串里的首次出现php 字符串 函数 转换字符串第一个字节为 0 255 之间的值php xml 解析器函数 将用 utf 8 方式编码的 iso 8859 1 字符串转换成单字节的 iso 8859 1 字符串。php 多字节字符串 函数重载功能
关注编程学问公众号