ord

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

ord轉(zhuǎn)換字符串第一個字節(jié)為 0-255 之間的值

說明

ord(string $string): int

解析 string 二進(jìn)制值第一個字節(jié)為 0 到 255 范圍的無符號整型類型。

如果字符串是 ASCII、 ISO-8859、Windows 1252之類單字節(jié)編碼,就等于返回該字符在字符集編碼表中的位置。 但請注意,本函數(shù)不會去檢測字符串的編碼,尤其是不會識別類似 UTF-8 或 UTF-16 這種多字節(jié)字符的 Unicode 代碼點(diǎn)(code point)。

該函數(shù)是 chr() 的互補(bǔ)函數(shù)。

參數(shù)

string

一個字符。

返回值

返回 0 - 255 的整型值。

范例

示例 #1 ord() 范例

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

示例 #2 檢查 UTF-8 字符串的每一個字節(jié)

<?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

參見