Ord - Manual - PHP

  • Downloads
  • Documentation
  • Get Involved
  • Help
  • PHP 8.4
Search docs Getting Started Introduction A simple tutorial Language Reference Basic syntax Types Variables Constants Expressions Operators Control Structures Functions Classes and Objects Namespaces Enumerations Errors Exceptions Fibers Generators Attributes References Explained Predefined Variables Predefined Exceptions Predefined Interfaces and Classes Predefined Attributes Context options and parameters Supported Protocols and Wrappers Security Introduction General considerations Installed as CGI binary Installed as an Apache module Session Security Filesystem Security Database Security Error Reporting User Submitted Data Hiding PHP Keeping Current Features HTTP authentication with PHP Cookies Sessions Handling file uploads Using remote files Connection handling Persistent Database Connections Command line usage Garbage Collection DTrace Dynamic Tracing Function Reference Affecting PHP's Behaviour Audio Formats Manipulation Authentication Services Command Line Specific Extensions Compression and Archive Extensions Cryptography Extensions Database Extensions Date and Time Related Extensions File System Related Extensions Human Language and Character Encoding Support Image Processing and Generation Mail Related Extensions Mathematical Extensions Non-Text MIME Output Process Control Extensions Other Basic Extensions Other Services Search Engine Extensions Server Specific Extensions Session Extensions Text Processing Variable and Type Related Extensions Web Services Windows Only Extensions XML Manipulation GUI Extensions Keyboard Shortcuts? This help j Next menu item k Previous menu item g p Previous man page g n Next man page G Scroll to bottom g g Scroll to top g h Goto homepage g s Goto search(current page) / Focus search box parse_str » « number_format
  • PHP Manual
  • Function Reference
  • Text Processing
  • Strings
  • String Functions
Change language: English German Spanish French Italian Japanese Brazilian Portuguese Russian Turkish Ukrainian Chinese (Simplified) Other ord

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

ordConvert the first byte of a string to a value between 0 and 255

Description

ord(string $character): int

Interprets the binary value of the first byte of character as an unsigned integer between 0 and 255.

If the string is in a single-byte encoding, such as ASCII, ISO-8859, or Windows 1252, this is equivalent to returning the position of a character in the character set's mapping table. However, note that this function is not aware of any string encoding, and in particular will never identify a Unicode code point in a multi-byte encoding such as UTF-8 or UTF-16.

This function complements chr().

Parameters

character

A character.

Return Values

An integer between 0 and 255.

Examples

Example #1 ord() example

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

Example #2 Examining the individual bytes of a UTF-8 string

<?phpdeclare(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;}?>

The above example will output:

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

See Also

  • chr() - Generate a single-byte string from a number
  • An » ASCII-table
  • mb_ord() - Get Unicode code point of character
  • IntlChar::ord() - Return Unicode code point value of character

Found A Problem?

Learn How To Improve This Page • Submit a Pull Request • Report a Bug +add a note

User Contributed Notes 6 notes

up down 47 arglanir+phpnet at gmail dot com12 years ago As ord() doesn't work with utf-8, and if you do not have access to mb_* functions, the following function will work well:<?phpfunction 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:<?php$text = "abcàê߀abc";$offset = 0;while ($offset >= 0) { echo $offset.": ".ordutf8($text, $offset)."\n";}/* returns:0: 971: 982: 993: 2245: 2347: 2239: 836412: 9713: 9814: 99*/?>Feel free to adapt my code to fit your needs. up down 7 paco at olecode dot com4 years ago this function convert UTF-8 string to RTF code string. I am using code of v0rbiz at yahoo dot com, thanks!!! function cadena_rtf($txt) { $result = null; for ($pos = 0; $pos < mb_strlen($txt); $pos++) { $char = mb_substr($txt, $pos, 1); if (!preg_match("/[A-Za-z1-9,.]/", $char)) { //unicode ord real!!! $k = mb_convert_encoding($char, 'UCS-2LE', 'UTF-8'); $k1 = ord(substr($k, 0, 1)); $k2 = ord(substr($k, 1, 1)); $ord = $k2 * 256 + $k1; if ($ord > 255) { $result .= '\uc1\u' . $ord . '*'; } elseif ($ord > 32768) { $result .= '\uc1\u' . ($ord - 65535) . '*'; } else { $result .= "\\'" . dechex($ord); } } else { $result .= $char; } } return $result; } up down 18 rowan dot collins at cwtdigital dot com11 years ago 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. up down 8 v0rbiz at yahoo dot com20 years ago I did not found a unicode/multibyte capable 'ord' function, so... <?php function uniord($u) { $k = mb_convert_encoding($u, 'UCS-2LE', 'UTF-8'); $k1 = ord(substr($k, 0, 1)); $k2 = ord(substr($k, 1, 1)); return $k2 * 256 + $k1; } ?> up down -1 Noname2 years ago <?phpdeclare (encoding='UTF-8');$animalsstr = '🐀🐁🐂🐃🐄🐅🐆🐇🐈🐉🐊🐋🐌🐍🐎🐏🐐🐑🐒🐓🐔🐕🐖🐗🐘🐙' . '🐚🐛🐜🐝🐞🐟🐠🐡🐢🐣🐤🐥🐦🐧🐨🐩🐪🐫🐬🐭🐮🐯🐰🐱🐲🐳🐴🐵' . '🐶🐷🐸🐹🐺🐻🐼🐽🐾🐿';$animals = mb_str_split($animalsstr);foreach ($animals as $animal) { for ($pos = 0; $pos < strlen($animal); $pos++) { $byte = substr($animal, $pos); echo "Byte $pos of $animal has value " . ord($byte) . PHP_EOL; }}?> up down -3 Anonymous3 years ago For anyone who's looking to convert full strings to map and back it's pretty simple but takes some getting used to...the code below saves an hour of scrounging codes for beginners like myself.function var2map($a) { $b=''; $c=strlen($a); for($i=0; $i<$c; ++$i) { $d=ord(substr($a,$i,1)); if($d<10) { $e='00'.$d; } else { if($d<100) { $e='0'.$d; } else { $e=$d; } } if($b=='') { $b=$e; } else { $b=$b.$e; } } return $b;}function map2var($a) { $b=''; $c=strlen($a) / 3; for($i=0; $i<$c; ++$i) { $d=chr(substr($a,$i*3,3)); if($b=='') { $b=$d; } else { $b=$b.$d; } } return $b;} +add a note
  • String Functions
    • addcslashes
    • addslashes
    • bin2hex
    • chop
    • chr
    • chunk_​split
    • convert_​uudecode
    • convert_​uuencode
    • count_​chars
    • crc32
    • crypt
    • echo
    • explode
    • fprintf
    • get_​html_​translation_​table
    • hebrev
    • hex2bin
    • html_​entity_​decode
    • htmlentities
    • htmlspecialchars
    • htmlspecialchars_​decode
    • implode
    • join
    • lcfirst
    • levenshtein
    • localeconv
    • ltrim
    • md5
    • md5_​file
    • metaphone
    • money_​format
    • nl_​langinfo
    • nl2br
    • number_​format
    • ord
    • parse_​str
    • print
    • printf
    • quoted_​printable_​decode
    • quoted_​printable_​encode
    • quotemeta
    • rtrim
    • setlocale
    • sha1
    • sha1_​file
    • similar_​text
    • soundex
    • sprintf
    • sscanf
    • str_​contains
    • str_​decrement
    • str_​ends_​with
    • str_​getcsv
    • str_​increment
    • str_​ireplace
    • str_​pad
    • str_​repeat
    • str_​replace
    • str_​rot13
    • str_​shuffle
    • str_​split
    • str_​starts_​with
    • str_​word_​count
    • strcasecmp
    • strchr
    • strcmp
    • strcoll
    • strcspn
    • strip_​tags
    • stripcslashes
    • stripos
    • stripslashes
    • stristr
    • strlen
    • strnatcasecmp
    • strnatcmp
    • strncasecmp
    • strncmp
    • strpbrk
    • strpos
    • strrchr
    • strrev
    • strripos
    • strrpos
    • strspn
    • strstr
    • strtok
    • strtolower
    • strtoupper
    • strtr
    • substr
    • substr_​compare
    • substr_​count
    • substr_​replace
    • trim
    • ucfirst
    • ucwords
    • vfprintf
    • vprintf
    • vsprintf
    • wordwrap
  • Deprecated
    • convert_​cyr_​string
    • hebrevc
    • utf8_​decode
    • utf8_​encode
To Top ↑ and ↓ to navigate • Enter to select • Esc to close Press Enter without selection to search using Google

Từ khóa » Hàm Ord Trong Php