When working with PHP, you may need to extract structured values from text using the current message pattern. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.
MessageFormatter::parse: Parsing reverses compatible formatting, but only for text that fits the ICU pattern.
What Is MessageFormatter::parse in PHP?
Extract structured values from text using the current message pattern. Parsing reverses compatible formatting, but only for text that fits the ICU pattern.
Syntax:
$formatter->parse(string $string): array|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: Parsing may return false. Not all formatted patterns produce an unambiguous reversible representation.
Example 1: Basic Usage
This example introduces the operation with a short and predictable input.
<?php
$formatter = new MessageFormatter("en_US", "Total: {0, number}");
print_r($formatter->parse("Total: 42"));
The code focuses on the main operation. Parsing reverses compatible formatting, but only for text that fits the ICU pattern.
Example 2: Another Case
Now compare a different input, locale, date, or option to see another part of the behavior.
<?php
$formatter = new MessageFormatter("en_US", "Name: {0}");
print_r($formatter->parse("Name: Alex"));
What changes? Parsing may return false. Not all formatted patterns produce an unambiguous reversible representation.
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
$formatter = new MessageFormatter("en_US", "Order: {0, number}");
foreach (["Order: 12", "invalid"] as $record) {
$values = $formatter->parse($record);
var_dump($values);
}
The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. Parsing reverses compatible formatting, but only for text that fits the ICU pattern.
Important Behavior and Edge Cases
- Core behavior: Parsing reverses compatible formatting, but only for text that fits the ICU pattern.
- Inputs and results: Parsing may return false. Not all formatted patterns produce an unambiguous reversible representation.
- Alternatives: MessageFormatter::parseMessage() parses a single value with a supplied pattern.
- Compatibility: The intl extension is required.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
MessageFormatter::parse | Extract structured values from text using the current message pattern. |
| Related PHP approach | MessageFormatter::parseMessage() parses a single value with a supplied pattern. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using an incorrect input or option | Parsing may return false. Not all formatted patterns produce an unambiguous reversible representation. |
| Confusing this API with a related operation | MessageFormatter::parseMessage() parses a single value with a supplied pattern. |
| Assuming output is identical across environments | Check return values and required extensions or versions. The intl extension is required. |
Key Takeaways
- MessageFormatter::parse helps you extract structured values from text using the current message pattern.
- Parsing reverses compatible formatting, but only for text that fits the ICU pattern.
- Parsing may return false. Not all formatted patterns produce an unambiguous reversible representation.
Use MessageFormatter::parse when you need to extract structured values from text using the current message pattern. 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.