Skip to content

Commit eb10503

Browse files
authored
Add examples for mb_scrub() (#5811)
* Add examples for mb_scrub() The page had no example, which made it hard to see that the replacement happens in the string itself: terminals, browsers and fonts render an ill-formed byte sequence with a replacement character of their own, so visual output alone is misleading. Both examples use bin2hex() or var_dump() so the reader sees the bytes rather than the rendering. The first example also shows that the result depends on the substitute character: mbstring.substitute_character defaults to "?" (0x3F), so the scrubbed string is 41 3f 42, and 41 ef bf bd 42 only after calling mb_substitute_character(0xFFFD). The second example shows the practical use: a PCRE pattern with the u modifier fails on the raw input and succeeds once it has been scrubbed. A see also section is added, since the page had none and the substitute character is a prerequisite for reading the first example. Outputs verified on PHP 8.1, 8.4 and 8.5. Fixes: #5562 * Unwrap examples and see also list from para
1 parent d3a03f8 commit eb10503

1 file changed

Lines changed: 78 additions & 0 deletions

File tree

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,84 @@
7171
</informaltable>
7272
</refsect1>
7373

74+
<refsect1 role="examples">
75+
&reftitle.examples;
76+
<example>
77+
<title>Byte-level replacement performed by <function>mb_scrub</function></title>
78+
<simpara>
79+
<function>bin2hex</function> is used here because terminals, browsers and
80+
fonts may render an ill-formed byte sequence with a replacement character
81+
of their own, which hides what the string actually contains.
82+
</simpara>
83+
<programlisting role="php">
84+
<![CDATA[
85+
<?php
86+
87+
// The byte 0xFF cannot appear in a valid UTF-8 string.
88+
$input = "A\xFFB";
89+
echo bin2hex($input), "\n";
90+
91+
// The default substitute character is "?" (0x3F).
92+
echo bin2hex(mb_scrub($input, 'UTF-8')), "\n";
93+
94+
// U+FFFD REPLACEMENT CHARACTER is encoded as EF BF BD in UTF-8.
95+
mb_substitute_character(0xFFFD);
96+
echo bin2hex(mb_scrub($input, 'UTF-8')), "\n";
97+
98+
?>
99+
]]>
100+
</programlisting>
101+
&example.outputs;
102+
<screen>
103+
<![CDATA[
104+
41ff42
105+
413f42
106+
41efbfbd42
107+
]]>
108+
</screen>
109+
</example>
110+
<example>
111+
<title>Using <function>mb_scrub</function> before UTF-8 aware processing</title>
112+
<simpara>
113+
PCRE patterns using the <literal>u</literal> modifier reject subjects that
114+
are not well-formed UTF-8. Scrubbing the input first makes it acceptable.
115+
</simpara>
116+
<programlisting role="php">
117+
<![CDATA[
118+
<?php
119+
120+
$input = "A\xFFB";
121+
122+
var_dump(preg_match_all('/./us', $input));
123+
echo preg_last_error_msg(), "\n";
124+
125+
$clean = mb_scrub($input, 'UTF-8');
126+
127+
var_dump(preg_match_all('/./us', $clean));
128+
129+
?>
130+
]]>
131+
</programlisting>
132+
&example.outputs;
133+
<screen>
134+
<![CDATA[
135+
bool(false)
136+
Malformed UTF-8 characters, possibly incorrectly encoded
137+
int(3)
138+
]]>
139+
</screen>
140+
</example>
141+
</refsect1>
142+
143+
<refsect1 role="seealso">
144+
&reftitle.seealso;
145+
<simplelist>
146+
<member><function>mb_substitute_character</function></member>
147+
<member><function>mb_check_encoding</function></member>
148+
<member><function>mb_convert_encoding</function></member>
149+
</simplelist>
150+
</refsect1>
151+
74152
</refentry>
75153
<!-- Keep this comment at the end of the file
76154
Local variables:

0 commit comments

Comments
 (0)