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 字符串 函数 函数把包含数据的二进制字符串转换为十六进制值php 字符串 函数 将字符由一种 cyrillic 字符转换成另一种php 字符串 函数 返回使用 htmlspecialchars 和 htmlentities 后的转换表php 多字节字符串 函数 对字符串进行大小写转换php 多字节字符串 函数 转换字符的编码php 多字节字符串 函数 转换一个或多个变量的字符编码php 多字节字符串 函数 get aliases of a known encoding typephp 多字节字符串 函数 regular expression match for multibyte stringphp 多字节字符串 函数 perform a regular expression search and replace with multibyte support using a callbackphp 多字节字符串 函数 设置/获取 http 输出字符编码php 多字节字符串 函数 设置/获取内部字符编码php 多字节字符串 函数 设置/获取当前的语言php 多字节字符串 函数 在输出缓冲中转换字符编码的回调函数php 多字节字符串 函数 大小写不敏感地查找字符串在另一个字符串里的首次出现php 多字节字符串 函数 获取字符串的长度php 字符串 函数 转换字符串第一个字节为 0 255 之间的值php 字符串 函数 将 quoted printable 字符串转换为 8 bit 字符串php 字符串 函数 将 8 bit 字符串转换成 quoted printable 字符串php xml 解析器函数 将用 utf 8 方式编码的 iso 8859 1 字符串转换成单字节的 iso 8859 1 字符串。php 多字节字符串 多字节字符串 函数
关注编程学问公众号