Skip to content

Commit 97f8edc

Browse files
authored
Clarify that mb_strpos returns a character position, not a byte position (#5853)
* Clarify mb_strpos returns character position, not byte; add example * Clarify offset parameter is in characters
1 parent 815ee76 commit 97f8edc

1 file changed

Lines changed: 36 additions & 9 deletions

File tree

‎reference/mbstring/functions/mb-strpos.xml‎

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@
5252
<varlistentry>
5353
<term><parameter>offset</parameter></term>
5454
<listitem>
55-
<para>
56-
The search offset. If it is not specified, 0 is used.
55+
<simpara>
56+
The search offset in characters. If it is not specified, 0 is used.
5757
A negative offset counts from the end of the string.
58-
</para>
58+
</simpara>
5959
</listitem>
6060
</varlistentry>
6161
<varlistentry>
@@ -70,12 +70,12 @@
7070

7171
<refsect1 role="returnvalues">
7272
&reftitle.returnvalues;
73-
<para>
74-
Returns the numeric position of
75-
the first occurrence of <parameter>needle</parameter> in the
76-
<parameter>haystack</parameter> <type>string</type>. If
77-
<parameter>needle</parameter> is not found, it returns &false;.
78-
</para>
73+
<simpara>
74+
Returns the character position (not byte position) of the first occurrence
75+
of <parameter>needle</parameter> in the <parameter>haystack</parameter>
76+
<type>string</type>, counting from <literal>0</literal>.
77+
Returns &false; if <parameter>needle</parameter> is not found.
78+
</simpara>
7979
</refsect1>
8080

8181
<refsect1 role="errors">
@@ -115,10 +115,37 @@
115115
</informaltable>
116116
</refsect1>
117117

118+
<refsect1 role="examples">
119+
&reftitle.examples;
120+
<example>
121+
<title><function>mb_strpos</function> returns a character offset, not a byte offset</title>
122+
<programlisting role="php">
123+
<![CDATA[
124+
<?php
125+
// "🐘" is 4 bytes but 1 character; "é" is 2 bytes but 1 character
126+
$str = "🐘H🐘é";
127+
128+
var_dump(mb_strpos($str, "é")); // character 3, not byte 9
129+
var_dump(mb_strpos($str, "🐘", 1)); // character 2, skipping the first "🐘"
130+
?>
131+
]]>
132+
</programlisting>
133+
&example.outputs;
134+
<screen>
135+
<![CDATA[
136+
int(3)
137+
int(2)
138+
]]>
139+
</screen>
140+
</example>
141+
</refsect1>
142+
118143
<refsect1 role="seealso">
119144
&reftitle.seealso;
120145
<para>
121146
<simplelist>
147+
<member><function>mb_strrpos</function></member>
148+
<member><function>mb_stripos</function></member>
122149
<member><function>mb_internal_encoding</function></member>
123150
<member><function>strpos</function></member>
124151
</simplelist>

0 commit comments

Comments
 (0)