Php функция ascii код символов
ord — Конвертирует первый байт строки в число от 0 до 255
Описание
Интерпретирует бинарное значение первого байта строки character как беззнаковое целое.
Если строка создана в однобайтовой кодировке, такой как ASCII, ISO-8859 или Windows 1252, результат функции будет эквивалентен позиции символа в соответствующей таблице кодировки. В любом случае, эта функция ничего не знает про кодировки и не сможет вернуть кодовую точку первого символа строки, закодированной в многобайтовой кодировке, такой как UTF-8 или UTF-16.
Эта функция дополняет функцию chr() .
Список параметров
Возвращаемые значения
Примеры
Пример #1 Пример использования ord()
Пример #2 Просмотр индивидуальный байтов строки UTF-8
declare( encoding = ‘UTF-8’ );
$str = «🐘» ;
for ( $pos = 0 ; $pos < strlen ( $str ); $pos ++ ) $byte = substr ( $str , $pos );
echo ‘Байт ‘ . $pos . ‘ строки $str равен ‘ . ord ( $byte ) . PHP_EOL ;
>
?>?php
Результат выполнения данного примера:
Байт 0 строки $str равен 240
Байт 1 строки $str равен 159
Байт 2 строки $str равен 144
Байт 3 строки $str равен 152
Смотрите также
- chr() — Генерирует односимвольную строку по заданному числу
- » Таблица ASCII-кодов
- mb_ord() — Получает кодовую точку символа Unicode
- IntlChar::ord() — Получить код символ Unicode
User Contributed Notes 6 notes
As ord() doesn’t work with utf-8, and if you do not have access to mb_* functions, the following function will work well:
function ordutf8 ( $string , & $offset ) $code = ord ( substr ( $string , $offset , 1 ));
if ( $code >= 128 ) < //otherwise 0xxxxxxx
if ( $code < 224 ) $bytesnumber = 2 ; //110xxxxx
else if ( $code < 240 ) $bytesnumber = 3 ; //1110xxxx
else if ( $code < 248 ) $bytesnumber = 4 ; //11110xxx
$codetemp = $code — 192 — ( $bytesnumber > 2 ? 32 : 0 ) — ( $bytesnumber > 3 ? 16 : 0 );
for ( $i = 2 ; $i <= $bytesnumber ; $i ++) $offset ++;
$code2 = ord ( substr ( $string , $offset , 1 )) — 128 ; //10xxxxxx
$codetemp = $codetemp * 64 + $code2 ;
>
$code = $codetemp ;
>
$offset += 1 ;
if ( $offset >= strlen ( $string )) $offset = — 1 ;
return $code ;
>
?>
$offset is a reference, as it is not easy to split a utf-8 char-by-char. Useful to iterate on a string:
$text = «abcàê߀abc» ;
$offset = 0 ;
while ( $offset >= 0 ) echo $offset . «: » . ordutf8 ( $text , $offset ). «\n» ;
>
/* returns:
0: 97
1: 98
2: 99
3: 224
5: 234
7: 223
9: 8364
12: 97
13: 98
14: 99
*/
?>
Feel free to adapt my code to fit your needs.
Regarding character sets, and whether or not this is «ASCII». Firstly, there is no such thing as «8-bit ASCII», so if it were ASCII it would only ever return integers up to 127. 8-bit ASCII-compatible encodings include the ISO 8859 family of encodings, which map various common characters to the values from 128 to 255. UTF-8 is also designed so that characters representable in 7-bit ASCII are coded the same; byte values higher than 127 in a UTF-8 string represent the beginning of a multi-byte character.
In fact, like most of PHP’s string functions, this function isn’t doing anything to do with character encoding at all — it is just interpreting a binary byte from a string as an unsigned integer. That is, ord(chr(200)) will always return 200, but what character chr(200) *means* will vary depending on what character encoding it is *interpreted* as part of (e.g. during display).
A technically correct description would be «Returns an integer representation of the first byte of a string, from 0 to 255. For single-byte encodings such as (7-bit) ASCII and the ISO 8859 family, this will correspond to the first character, and will be the position of that character in the encoding’s mapping table. For multi-byte encodings, such as UTF-8 or UTF-16, the byte may not represent a complete character.»
The link to asciitable.com should also be replaced by one which explains what character encoding it is displaying, as «Extended ASCII» is an ambiguous and misleading name.