When working with PHP, you may need to apply a transliteration rule to text. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.
Transliterator::transliterate: Conversion can transliterate scripts or simplify diacritics, depending on the selected ICU rules.
What Is Transliterator::transliterate in PHP?
Apply a transliteration rule to text. Conversion can transliterate scripts or simplify diacritics, depending on the selected ICU rules.
Syntax:
$transliterator->transliterate(string $string, int $start = 0, int $end = -1): 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: Transliteration is not reversible. Do not use it to silently overwrite original person or place names.
Example 1: Basic Usage
This example introduces the operation with a short and predictable input.
<?php
$transliterator = Transliterator::create("Any-Latin; Latin-ASCII");
echo $transliterator->transliterate("Café");
The code focuses on the main operation. Conversion can transliterate scripts or simplify diacritics, depending on the selected ICU rules.
Example 2: Another Case
Now compare a different input, locale, date, or option to see another part of the behavior.
<?php
$transliterator = Transliterator::create("Any-Latin; Latin-ASCII");
echo $transliterator->transliterate("München");
What changes? Transliteration is not reversible. Do not use it to silently overwrite original person or place names.
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
$transliterator = Transliterator::create("Any-Latin; Latin-ASCII");
$names = ["François", "Zoë", "Łódź"];
foreach ($names as $name) {
echo $transliterator->transliterate($name) . PHP_EOL;
}
The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. Conversion can transliterate scripts or simplify diacritics, depending on the selected ICU rules.
Important Behavior and Edge Cases
- Core behavior: Conversion can transliterate scripts or simplify diacritics, depending on the selected ICU rules.
- Inputs and results: Transliteration is not reversible. Do not use it to silently overwrite original person or place names.
- Alternatives: transliterator_transliterate() provides a procedural alternative.
- Compatibility: The intl extension is required.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
Transliterator::transliterate | Apply a transliteration rule to text. |
| Related PHP approach | transliterator_transliterate() provides a procedural alternative. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using an incorrect input or option | Transliteration is not reversible. Do not use it to silently overwrite original person or place names. |
| Confusing this API with a related operation | transliterator_transliterate() provides a procedural alternative. |
| Assuming output is identical across environments | Check return values and required extensions or versions. The intl extension is required. |
Key Takeaways
- Transliterator::transliterate helps you apply a transliteration rule to text.
- Conversion can transliterate scripts or simplify diacritics, depending on the selected ICU rules.
- Transliteration is not reversible. Do not use it to silently overwrite original person or place names.
Use Transliterator::transliterate when you need to apply a transliteration rule to text. 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.