Content · Programming · PHP

PHP MessageFormatter::parseMessage() Method: Usage and Examples

Learn how to parse an ICU message with a one-off pattern in PHP with syntax, clear code examples, practical use, and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

When working with PHP, you may need to parse an ICU message with a one-off pattern. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.

MessageFormatter::parseMessage: The static method combines pattern construction and parsing.

What Is MessageFormatter::parseMessage in PHP?

Parse an ICU message with a one-off pattern. The static method combines pattern construction and parsing.

Syntax:

MessageFormatter::parseMessage(string $locale, string $pattern, string $message): 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: Always test failure before reading parsed indexes; parsing is not input validation for arbitrary formats.

Example 1: Basic Usage

This example introduces the operation with a short and predictable input.

<?php

print_r(MessageFormatter::parseMessage("en_US", "Count: {0, number}", "Count: 24"));

The code focuses on the main operation. The static method combines pattern construction and parsing.

Example 2: Another Case

Now compare a different input, locale, date, or option to see another part of the behavior.

<?php

var_dump(MessageFormatter::parseMessage("en_US", "Count: {0, number}", "Unknown"));

What changes? Always test failure before reading parsed indexes; parsing is not input validation for arbitrary formats.

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

$lines = ["Amount: 10", "Amount: 20"];
foreach ($lines as $line) {
    $values = MessageFormatter::parseMessage("en_US", "Amount: {0, number}", $line);
    if ($values !== false) {
        echo $values[0] . PHP_EOL;
    }
}

The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. The static method combines pattern construction and parsing.

Important Behavior and Edge Cases

  • Core behavior: The static method combines pattern construction and parsing.
  • Inputs and results: Always test failure before reading parsed indexes; parsing is not input validation for arbitrary formats.
  • Alternatives: MessageFormatter::parse() reuses a formatter instance.
  • Compatibility: The intl extension is required.

Comparison With Related Functions

Function or ApproachWhen to Use It
MessageFormatter::parseMessageParse an icu message with a one-off pattern.
Related PHP approachMessageFormatter::parse() reuses a formatter instance.

Common Beginner Mistakes

MistakeCorrect Approach
Using an incorrect input or optionAlways test failure before reading parsed indexes; parsing is not input validation for arbitrary formats.
Confusing this API with a related operationMessageFormatter::parse() reuses a formatter instance.
Assuming output is identical across environmentsCheck return values and required extensions or versions. The intl extension is required.

Key Takeaways

  • MessageFormatter::parseMessage helps you parse an ICU message with a one-off pattern.
  • The static method combines pattern construction and parsing.
  • Always test failure before reading parsed indexes; parsing is not input validation for arbitrary formats.

Use MessageFormatter::parseMessage when you need to parse an ICU message with a one-off 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.

Share this resourceSend this page to someone who may find it useful.