When working with PHP, you may need to convert text to a standard Unicode normalization form. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.
Normalizer::normalize: Composed and decomposed representations may render identically but compare differently byte by byte.
What Is Normalizer::normalize in PHP?
Convert text to a standard Unicode normalization form. Composed and decomposed representations may render identically but compare differently byte by byte.
Syntax:
Normalizer::normalize(string $string, int $form = Normalizer::FORM_C): string|false
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: The function can return false for invalid input. Normalization is not the same as transliteration or accent stripping.
Example 1: Basic Usage
This example introduces the operation with a short and predictable input.
<?php
echo Normalizer::normalize("e\u{0301}", Normalizer::FORM_C);
The code focuses on the main operation. Composed and decomposed representations may render identically but compare differently byte by byte.
Example 2: Another Case
Now compare a different input, locale, date, or option to see another part of the behavior.
<?php
var_dump(Normalizer::normalize("é", Normalizer::FORM_D));
What changes? The function can return false for invalid input. Normalization is not the same as transliteration or accent stripping.
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
$labels = ["Café", "Cafe\u{0301}"];
$keys = array_map(fn($label) => Normalizer::normalize($label, Normalizer::FORM_C), $labels);
print_r(array_unique($keys));
The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. Composed and decomposed representations may render identically but compare differently byte by byte.
Important Behavior and Edge Cases
- Core behavior: Composed and decomposed representations may render identically but compare differently byte by byte.
- Inputs and results: The function can return false for invalid input. Normalization is not the same as transliteration or accent stripping.
- Alternatives: Normalizer::isNormalized() tests whether conversion is needed.
- Compatibility: The intl extension is required.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
Normalizer::normalize | Convert text to a standard unicode normalization form. |
| Related PHP approach | Normalizer::isNormalized() tests whether conversion is needed. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using an incorrect input or option | The function can return false for invalid input. Normalization is not the same as transliteration or accent stripping. |
| Confusing this API with a related operation | Normalizer::isNormalized() tests whether conversion is needed. |
| Assuming output is identical across environments | Check return values and required extensions or versions. The intl extension is required. |
Key Takeaways
- Normalizer::normalize helps you convert text to a standard Unicode normalization form.
- Composed and decomposed representations may render identically but compare differently byte by byte.
- The function can return false for invalid input. Normalization is not the same as transliteration or accent stripping.
Use Normalizer::normalize when you need to convert text to a standard Unicode normalization form. 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.