string chr [ int ascii]
int ord [ string string]
The American Standard Code for Information Interchange is a special set of 255 numbers that evaluate to letters, symbols, and actions used in most computers. For example, 74 is "J", 106 is "j", 123 is {, and 32 is a space. To convert to ASCII from textual characters, you should use the chr[] function, which takes an ASCII value as its only parameter and returns the text equivalent if there is one. The ord[] function does the opposite - it takes a string and returns the equivalent ASCII value.
For example:
That should output the following:
The second letter in the string is S
ASCII number 109 is equivalent to m
Want to learn PHP 7?
Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!
If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!
Next chapter: Measuring strings >>
Previous chapter: Replacing parts of a string
Jump to:
Home: Table of Contents
Copyright ©2015 Paul Hudson. Follow me: @twostraws.
[PHP 4, PHP 5, PHP 7, PHP 8]
ord — Convert the first byte of a string to a value between 0 and 255
Description
ord[string $character
]: int
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
Example #2 Examining the individual bytes of a UTF-8 string
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
arglanir+phpnet at gmail dot com ¶
10 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:
$offset is a reference, as it is not easy to split a utf-8 char-by-char. Useful to iterate on a string:
Feel free to adapt my code to fit your needs.
rowan dot collins at cwtdigital dot com ¶
9 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.
paco at olecode dot com ¶
2 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;
}
v0rbiz at yahoo dot com ¶
18 years ago
I did not found a unicode/multibyte capable 'ord' function, so...
Noname ¶
8 months ago
Anonymous ¶
1 year 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