When working with PHP, you may need to replace invalid characters in a multibyte string. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.
mb_scrub: Scrubbing follows the configured substitute-character policy for invalid encoded sequences.
What Is mb_scrub in PHP?
Replace invalid characters in a multibyte string. Scrubbing follows the configured substitute-character policy for invalid encoded sequences.
Syntax:
mb_scrub(string $string, ?string $encoding = null): string
Parameters and return value: The signature above identifies the arguments and return type where the API has a single call. For topic guides, follow the sequence of calls in the examples. Always distinguish a returned value from an object or variable modified by reference.
Important: Replacing invalid sequences loses information. Prefer rejecting malformed input when exact original text is required.
Example 1: Basic Usage
This example introduces the operation with a short and predictable input.
<?php
echo mb_scrub("Hello", "UTF-8");
The code focuses on the main operation. Scrubbing follows the configured substitute-character policy for invalid encoded sequences.
Example 2: Another Case
Now compare a different input, locale, date, or option to see another part of the behavior.
<?php
$invalidText = "Hello\xFFworld";
echo mb_scrub($invalidText, "UTF-8");
What changes? Replacing invalid sequences loses information. Prefer rejecting malformed input when exact original text is required.
Practical Example: Applying It in an Application
Use the following separate example as a starting point for a small application task. Check any extension or environment requirements below before running it.
<?php
$incomingText = "Café\xFF";
if (!mb_check_encoding($incomingText, "UTF-8")) {
$incomingText = mb_scrub($incomingText, "UTF-8");
}
echo $incomingText;
The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. Scrubbing follows the configured substitute-character policy for invalid encoded sequences.
Important Behavior and Edge Cases
- Core behavior: Scrubbing follows the configured substitute-character policy for invalid encoded sequences.
- Inputs and results: Replacing invalid sequences loses information. Prefer rejecting malformed input when exact original text is required.
- Alternatives: mb_check_encoding() only validates; mb_scrub() modifies invalid sequences.
- Compatibility: mbstring is required for mb_* functions.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
mb_scrub | Replace invalid characters in a multibyte string. |
| Related PHP approach | mb_check_encoding() only validates; mb_scrub() modifies invalid sequences. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using an incorrect input or option | Replacing invalid sequences loses information. Prefer rejecting malformed input when exact original text is required. |
| Confusing this API with a related operation | mb_check_encoding() only validates; mb_scrub() modifies invalid sequences. |
| Assuming output is identical across environments | Check return values and required extensions or versions. mbstring is required for mb_* functions. |
Key Takeaways
- mb_scrub helps you replace invalid characters in a multibyte string.
- Scrubbing follows the configured substitute-character policy for invalid encoded sequences.
- Replacing invalid sequences loses information. Prefer rejecting malformed input when exact original text is required.
Use mb_scrub when you need to replace invalid characters in a multibyte string. Start with the first example, compare the second, and adapt the practical example to your inputs. Confirm the return value and environment requirements before adding it to production code.