When working with PHP, you may need to create a transliterator from custom transformation rules. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.
Transliterator::createFromRules: ICU transformation rules can replace or map text according to a configured rule set.
What Is Transliterator::createFromRules in PHP?
Create a transliterator from custom transformation rules. ICU transformation rules can replace or map text according to a configured rule set.
Syntax:
Transliterator::createFromRules(string $rules, 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: Rules are not regular expressions and must follow ICU transliteration syntax. Verify that creation succeeded.
Example 1: Basic Usage
This example introduces the operation with a short and predictable input.
<?php
$converter = Transliterator::createFromRules("a > b;");
echo $converter->transliterate("cat");
The code focuses on the main operation. ICU transformation rules can replace or map text according to a configured rule set.
Example 2: Another Case
Now compare a different input, locale, date, or option to see another part of the behavior.
<?php
$converter = Transliterator::createFromRules("ph > f;");
echo $converter->transliterate("phone");
What changes? Rules are not regular expressions and must follow ICU transliteration syntax. Verify that creation succeeded.
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
$rules = "ph > f; th > t;";
$converter = Transliterator::createFromRules($rules);
foreach (["phone", "theme"] as $word) {
echo $converter->transliterate($word) . PHP_EOL;
}
The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. ICU transformation rules can replace or map text according to a configured rule set.
Important Behavior and Edge Cases
- Core behavior: ICU transformation rules can replace or map text according to a configured rule set.
- Inputs and results: Rules are not regular expressions and must follow ICU transliteration syntax. Verify that creation succeeded.
- Alternatives: Transliterator::create() loads registered ICU transliterator IDs.
- Compatibility: The intl extension is required.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
Transliterator::createFromRules | Create a transliterator from custom transformation rules. |
| Related PHP approach | Transliterator::create() loads registered ICU transliterator IDs. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using an incorrect input or option | Rules are not regular expressions and must follow ICU transliteration syntax. Verify that creation succeeded. |
| Confusing this API with a related operation | Transliterator::create() loads registered ICU transliterator IDs. |
| Assuming output is identical across environments | Check return values and required extensions or versions. The intl extension is required. |
Key Takeaways
- Transliterator::createFromRules helps you create a transliterator from custom transformation rules.
- ICU transformation rules can replace or map text according to a configured rule set.
- Rules are not regular expressions and must follow ICU transliteration syntax. Verify that creation succeeded.
Use Transliterator::createFromRules when you need to create a transliterator from custom transformation rules. 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.