When working with PHP, you may need to create a Unicode transliterator using an ICU identifier. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.
Transliterator::create: Transliteration changes writing systems or character forms using ICU rules.
What Is Transliterator::create in PHP?
Create a Unicode transliterator using an ICU identifier. Transliteration changes writing systems or character forms using ICU rules.
Syntax:
Transliterator::create(string $id, int $direction = Transliterator::FORWARD): ?Transliterator
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: A transliteration can be lossy and should not replace the original display text in storage.
Example 1: Basic Usage
This example introduces the operation with a short and predictable input.
<?php
var_dump(Transliterator::create("Any-Latin; Latin-ASCII") instanceof Transliterator);
The code focuses on the main operation. Transliteration changes writing systems or character forms using 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? A transliteration can be lossy and should not replace the original display text in storage.
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");
$cities = ["München", "Zürich", "São Paulo"];
foreach ($cities as $city) {
echo $transliterator->transliterate($city) . PHP_EOL;
}
The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. Transliteration changes writing systems or character forms using ICU rules.
Important Behavior and Edge Cases
- Core behavior: Transliteration changes writing systems or character forms using ICU rules.
- Inputs and results: A transliteration can be lossy and should not replace the original display text in storage.
- Alternatives: Transliterator::createFromRules() creates a transliterator from custom rules.
- Compatibility: The intl extension is required.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
Transliterator::create | Create a unicode transliterator using an icu identifier. |
| Related PHP approach | Transliterator::createFromRules() creates a transliterator from custom rules. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using an incorrect input or option | A transliteration can be lossy and should not replace the original display text in storage. |
| Confusing this API with a related operation | Transliterator::createFromRules() creates a transliterator from custom rules. |
| Assuming output is identical across environments | Check return values and required extensions or versions. The intl extension is required. |
Key Takeaways
- Transliterator::create helps you create a Unicode transliterator using an ICU identifier.
- Transliteration changes writing systems or character forms using ICU rules.
- A transliteration can be lossy and should not replace the original display text in storage.
Use Transliterator::create when you need to create a Unicode transliterator using an ICU identifier. 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.