Add examples for mb_scrub() - #5811
Merged
Merged
Conversation
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: php#5562
jordikroon
reviewed
Aug 27, 2026
jordikroon
reviewed
Aug 27, 2026
This was referenced Aug 31, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
mb_scrub()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 therefore usebin2hex()orvar_dump(), so the reader sees the bytes rather than the rendering.Byte-level replacement. The first example also shows that the result depends on the substitute character.
mbstring.substitute_characterdefaults to"?"(0x3F), so scrubbing"A\xFFB"yields41 3f 42;41 ef bf bd 42is only produced after callingmb_substitute_character(0xFFFD). This differs from the output given in the issue, which assumed U+FFFD by default.Use before UTF-8 aware processing. The second example shows the practical case: a PCRE pattern with the
umodifier fails on the raw input withMalformed UTF-8 characters, possibly incorrectly encoded, and succeeds once the string 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.
Both
<screen>blocks were extracted from the file and compared byte for byte against the real output on PHP 8.1, 8.4 and 8.5.Fixes: #5562