- mb_ord
- Список параметров
- Возвращаемые значения
- Список изменений
- Примеры
- Смотрите также
- User Contributed Notes 1 note
- Получение кода символа php
- Описание
- Список параметров
- Возвращаемые значения
- Примеры
- Смотрите также
- User Contributed Notes 6 notes
- PHP: ord() function
- PHP: Tips of the Day
- Converting character to code and code to character
- The ord() function
- Protecting Email Address
- The chr() function
- mb_ord()
- mb_chr()
mb_ord
Возвращает значение кодовой точки Unicode для данного символа.
Список параметров
Параметр encoding представляет собой символьную кодировку. Если он опущен или равен null , вместо него будет использовано значение внутренней кодировки.
Возвращаемые значения
Кодовая точка Unicode для первого символа string или false в случае возникновения ошибки.
Список изменений
Примеры
var_dump ( mb_ord ( «A» , «UTF-8» ));
var_dump ( mb_ord ( «🐘» , «UTF-8» ));
var_dump ( mb_ord ( «\x80» , «ISO-8859-1» ));
var_dump ( mb_ord ( «\x80» , «Windows-1252» ));
?>?php
Результат выполнения данного примера:
Смотрите также
- mb_internal_encoding() — Установка/получение внутренней кодировки скрипта
- mb_chr() — Возвращает символ по значению кодовой точки Unicode
- IntlChar::ord() — Получить код символ Unicode
- ord() — Конвертирует первый байт строки в число от 0 до 255
User Contributed Notes 1 note
$array[‘Б’] = uniord(‘Б’);
$array[‘🚷’] = uniord(‘🚷’);
$array[‘mb_ord Б’] = mb_ord(‘Б’);
$array[‘mb_ord 🚷’] = mb_ord(‘🚷’);
function uniord($charUTF8)
$charUCS4 = mb_convert_encoding($charUTF8, ‘UCS-4BE’, ‘UTF-8’);
$byte1 = ord(substr($charUCS4, 0, 1));
$byte2 = ord(substr($charUCS4, 1, 1));
$byte3 = ord(substr($charUCS4, 2, 1));
$byte4 = ord(substr($charUCS4, 3, 1));
return ($byte1 >
array ( ‘Б’ => 1041, ‘🚷’ => 128695, ‘mb_ord Б’ => 1041, ‘mb_ord 🚷’ => 128695, )
https://unicode-table.com/en/0411/
Б
Encoding hex dec (bytes) dec binary
UTF-8 D0 91 208 145 53393 11010000 10010001
UTF-16BE 04 11 4 17 1041 00000100 00010001
UTF-16LE 11 04 17 4 4356 00010001 00000100
UTF-32BE 00 00 04 11 0 0 4 17 1041 00000000 00000000 00000100 00010001
UTF-32LE 11 04 00 00 17 4 0 0 285474816 00010001 00000100 00000000 00000000
https://unicode-table.com/en/1F6B7/
🚷
Encoding hex dec (bytes) dec binary
UTF-8 F0 9F 9A B7 240 159 154 183 4036991671 11110000 10011111 10011010 10110111
UTF-16BE D8 3D DE B7 216 61 222 183 3627933367 11011000 00111101 11011110 10110111
UTF-16LE 3D D8 B7 DE 61 216 183 222 1037613022 00111101 11011000 10110111 11011110
UTF-32BE 00 01 F6 B7 0 1 246 183 128695 00000000 00000001 11110110 10110111
UTF-32LE B7 F6 01 00 183 246 1 0 3086352640 10110111 11110110 00000001 00000000
- Функции для работы с многобайтовыми строками
- mb_check_encoding
- mb_chr
- mb_convert_case
- mb_convert_encoding
- mb_convert_kana
- mb_convert_variables
- mb_decode_mimeheader
- mb_decode_numericentity
- mb_detect_encoding
- mb_detect_order
- mb_encode_mimeheader
- mb_encode_numericentity
- mb_encoding_aliases
- mb_ereg_match
- mb_ereg_replace_callback
- mb_ereg_replace
- mb_ereg_search_getpos
- mb_ereg_search_getregs
- mb_ereg_search_init
- mb_ereg_search_pos
- mb_ereg_search_regs
- mb_ereg_search_setpos
- mb_ereg_search
- mb_ereg
- mb_eregi_replace
- mb_eregi
- mb_get_info
- mb_http_input
- mb_http_output
- mb_internal_encoding
- mb_language
- mb_list_encodings
- mb_ord
- mb_output_handler
- mb_parse_str
- mb_preferred_mime_name
- mb_regex_encoding
- mb_regex_set_options
- mb_scrub
- mb_send_mail
- mb_split
- mb_str_split
- mb_strcut
- mb_strimwidth
- mb_stripos
- mb_stristr
- mb_strlen
- mb_strpos
- mb_strrchr
- mb_strrichr
- mb_strripos
- mb_strrpos
- mb_strstr
- mb_strtolower
- mb_strtoupper
- mb_strwidth
- mb_substitute_character
- mb_substr_count
- mb_substr
Получение кода символа php
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.
PHP: ord() function
The ord() function is used to get the ASCII value of the first character of a string.
Return value
The ASCII value as an integer.
Value Type: Integer.
Pictorial Presentation
Example of php ord() function
Previous: number_format
Next: parse_strFollow us on Facebook and Twitter for latest update.
PHP: Tips of the Day
How can I sanitize user input with PHP?
It’s a common misconception that user input can be filtered. PHP even has a (now deprecated) «feature», called magic-quotes, that builds on this idea. It’s nonsense. Forget about filtering (or cleaning, or whatever people call it).
What you should do, to avoid problems, is quite simple: whenever you embed a string within foreign code, you must escape it, according to the rules of that language. For example, if you embed a string in some SQL targeting MySQL, you must escape the string with MySQL’s function for this purpose (mysqli_real_escape_string). (Or, in case of databases, using prepared statements are a better approach, when possible.)
Another example is HTML: If you embed strings within HTML markup, you must escape it with htmlspecialchars. This means that every single echo or print statement should use htmlspecialchars.
A third example could be shell commands: If you are going to embed strings (such as arguments) to external commands, and call them with exec, then you must use escapeshellcmd and escapeshellarg.
The only case where you need to actively filter data, is if you’re accepting preformatted input. For example, if you let your users post HTML markup, that you plan to display on the site. However, you should be wise to avoid this at all cost, since no matter how well you filter it, it will always be a potential security hole.
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook
Converting character to code and code to character
All characters, even if they are not available on your keyboard, have a character code. PHP offers four functions to deal with these characters code: ord() and chr() to deal with single-byte characters; mb_ord and mb_chr() to deal with multi-byte characters. The chr() and mb_chr() convert the numeric value into the corresponding character; ord() and mb_ord() returns the numeric value for characters.
The ord() function
The ord() function converts the first byte of a string to a value between 0 and 255. For example, a value is 97 and A value is 65 :
This function converts the single-byte characters, use the mb_ord() function if you are dealing with multi-byte characters.
Show ord() function result on Web Browsers
Use the HTML entities syntax and the web browser will automatically decode the number into the relevant character. For example, the ASCII value/code of B is 66 , the HTML entity B displays B on a browser page, see example:
Protecting Email Address
The following code can be used to protect email addresses from spammers. Using HTML entities for email addresses, making it much harder for spam bots to find email addresses from web pages:
The chr() function
The chr() function does the reverse of ord() function, it generates a single-byte string from a number (0-255), use mb_chr() function if you are dealing with multi-byte characters. In the following example, we used chr() function to generate the entire alphabet:
echo chr($a); > //ABCDEFGHIJKLMNOPQRSTUVWXYZ
mb_ord()
- $string : the input string
- $encoding (optional): the character encoding. If null or not provided, the internal character encoding value will be used.
The mb_ord() function returns the Unicode code point value of the given character. The code point value is a numerical value that maps to a specific character.
mb_chr()
- $codepoint : A Unicode codepoint value
- $encoding (optional): the character encoding. If null or not provided, the internal character encoding value will be used.
The mb_chr() function does the reverse of mb_ord() function, it generates a multi-byte string from a number (Unicode codepoint value), use chr() function if you are dealing with single-byte characters.
Working with Strings: