Skip to content

Add examples for mb_scrub() - #5811

Merged
lacatoire merged 2 commits into
php:masterfrom
lacatoire:mb-scrub-examples
Aug 27, 2026
Merged

Add examples for mb_scrub()#5811
lacatoire merged 2 commits into
php:masterfrom
lacatoire:mb-scrub-examples

Conversation

@lacatoire

Copy link
Copy Markdown
Member

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 use bin2hex() or var_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_character defaults to "?" (0x3F), so scrubbing "A\xFFB" yields 41 3f 42; 41 ef bf bd 42 is only produced after calling mb_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 u modifier fails on the raw input with Malformed 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

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
Comment thread reference/mbstring/functions/mb-scrub.xml Outdated
Comment thread reference/mbstring/functions/mb-scrub.xml Outdated
@lacatoire
lacatoire merged commit eb10503 into php:master Aug 27, 2026
2 checks passed
@lacatoire
lacatoire deleted the mb-scrub-examples branch August 27, 2026 19:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add basic examples for mb_scrub()

2 participants