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 多字节字符串 函数 对字符串进行大小写转换php 多字节字符串 函数 转换字符的编码php 多字节字符串 函数 convert kana one from another zen kaku han kaku and more php 多字节字符串 函数 转换一个或多个变量的字符编码php 多字节字符串 函数 解码 mime 头字段中的字符串php 多字节字符串 函数 returns position and length of a matched part of the multibyte regular expression for a predefined multibyte stringphp 多字节字符串 函数 returns the matched part of a multibyte regular expressionphp 多字节字符串 函数 set start point of next regular expression matchphp 多字节字符串 函数 multibyte regular expression match for predefined multibyte stringphp 多字节字符串 函数 在输出缓冲中转换字符编码的回调函数php 多字节字符串 函数 获取 mime 字符串php 多字节字符串 函数 set/get character encoding for multibyte regexphp 多字节字符串 函数 set/get the default options for mbregex functionsphp 多字节字符串 函数 descriptionphp 多字节字符串 函数 查找字符串在另一个字符串里的首次出现php 多字节字符串 函数 使字符串小写php 多字节字符串 函数 使字符串大写php 字符串 函数 转换字符串第一个字节为 0 255 之间的值php xml 解析器函数 将用 utf 8 方式编码的 iso 8859 1 字符串转换成单字节的 iso 8859 1 字符串。
关注编程学问公众号