reference/strings/functions/ord.xml
4d3d1ebea1eeecb7f76fb2fa8ecb55ef35d1d518
...
...
@@ -3,18 +3,21 @@
3
3
<refentry xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://docbook.org/ns/docbook" xml:id="function.ord">
4
4
<refnamediv>
5
5
<refname>ord</refname>
6
-
<refpurpose>Return ASCII value of character</refpurpose>
6
+
<refpurpose>Convert the first byte of a string to a value between 0 and 255</refpurpose>
7
7
</refnamediv>
8
8
9
9
<refsect1 role="description">
10
10
&reftitle.description;
11
11
<methodsynopsis>
12
12
<type>int</type><methodname>ord</methodname>
13
-
<methodparam><type>string</type><parameter>string</parameter></methodparam>
13
+
<methodparam><type>string</type><parameter>character</parameter></methodparam>
14
14
</methodsynopsis>
15
15
<para>
16
-
Returns the ASCII value of the first character of
17
-
<parameter>string</parameter>.
16
+
Interprets the binary value of the first byte of
17
+
<parameter>character</parameter> as an unsigned integer between 0 and 255.
18
+
</para>
19
+
<para>
20
+
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.
18
21
</para>
19
22
<para>
20
23
This function complements <function>chr</function>.
...
...
@@ -26,7 +29,7 @@
26
29
<para>
27
30
<variablelist>
28
31
<varlistentry>
29
-
<term><parameter>string</parameter></term>
32
+
<term><parameter>character</parameter></term>
30
33
<listitem>
31
34
<para>
32
35
A character.
...
...
@@ -40,7 +43,7 @@
40
43
<refsect1 role="returnvalues">
41
44
&reftitle.returnvalues;
42
45
<para>
43
-
Returns the ASCII value as an integer.
46
+
An integer between 0 and 255.
44
47
</para>
45
48
</refsect1>
46
49

...
...
@@ -60,6 +63,30 @@ if (ord($str) == 10) {
60
63
]]>
61
64
</programlisting>
62
65
</example>
66
+
</para>
67
+
<para>
68
+
<example>
69
+
<title>Examining the individual bytes of a UTF-8 string</title>
70
+
<programlisting role="php">
71
+
<![CDATA[
72
+
<?php
73
+
declare(encoding='UTF-8');
74
+
$str = "🐘";
75
+
for ( $pos=0; $pos < strlen($str); $pos ++ ) {
76
+
$byte = substr($str, $pos);
77
+
echo 'Byte ' . $pos . ' of $str has value ' . ord($byte) . PHP_EOL;
78
+
}
79
+
?>
80
+
]]>
81
+
</programlisting>
82
+
&example.outputs;
83
+
<screen>
84
+
Byte 0 of $str has value 240
85
+
Byte 1 of $str has value 159
86
+
Byte 2 of $str has value 144
87
+
Byte 3 of $str has value 152
88
+
</screen>
89
+
</example>
63
90
</para>
64
91
</refsect1>
65
92

...
...
@@ -69,12 +96,13 @@ if (ord($str) == 10) {
69
96
<simplelist>
70
97
<member><function>chr</function></member>
71
98
<member>An <link xlink:href="&url.asciitable;">ASCII-table</link></member>
99
+
<member><function>mb_ord</function></member>
100
+
<member><function>IntlChar::ord</function></member>
72
101
</simplelist>
73
102
</para>
74
103
</refsect1>
75
104

76
105
</refentry>
77
-

78
106
<!-- Keep this comment at the end of the file
79
107
Local variables:
80
108
mode: sgml
81
109