Every catalogue I have worked on builds its price ranges the same way: format the low number, format the high number, glue a hyphen in the middle. It reads fine in en_GB and it is wrong almost everywhere else — wrong dash, wrong spacing, currency symbol repeated when the locale would drop it.
The PHP intl number range formatter in 8.6 fixes that. IntlNumberRangeFormatter hands the whole interval to ICU and lets it decide, and the same release adds grapheme_strrev() for the other string problem nobody notices until a user types an emoji.
Check the ICU version behind your intl extension#
IntlNumberRangeFormatter is a thin wrapper over ICU's NumberRangeFormatter, and the PHP 8.6 new features page states it is supported as of ICU 63. The class exists whenever ext-intl is loaded on 8.6, but the ICU build underneath is what actually does the work, so check it before you write code against it. If you are still planning the move to 8.6, the PHP 8.6 upgrade checklist for Laravel covers the rest of the extension surface.
php -r 'echo PHP_VERSION, " / ICU ", INTL_ICU_VERSION, PHP_EOL;'
# 8.6.1 / ICU 74.2
// A guard worth having in a service provider or a health check.
if (! extension_loaded('intl') || version_compare(INTL_ICU_VERSION, '63.0', '<')) {
// Fall back to your old concatenation helper rather than fataling.
}
Build the range with the PHP intl number range formatter#
The formatter is created from an ICU number skeleton — the same compact string syntax ICU uses everywhere else — and not from a NumberFormatter instance. There is no usable constructor: new IntlNumberRangeFormatter(...) throws, and the static factory is the only entry point. All four arguments are required; there are no defaults to lean on.
$formatter = IntlNumberRangeFormatter::createFromSkeleton(
'currency/GBP', // ICU skeleton
'en_GB', // locale
IntlNumberRangeFormatter::COLLAPSE_AUTO, // collapse mode
IntlNumberRangeFormatter::IDENTITY_FALLBACK_APPROXIMATELY // equal endpoints
);
echo $formatter->format(20, 50);
format() takes two numbers — ints or floats — and returns the finished string. Swap the skeleton and the same object shape covers units and percentages:
// Distances: "1–5 km"
IntlNumberRangeFormatter::createFromSkeleton(
'measure-unit/length-kilometer unit-width-short',
'en_GB',
IntlNumberRangeFormatter::COLLAPSE_UNIT,
IntlNumberRangeFormatter::IDENTITY_FALLBACK_SINGLE_VALUE,
)->format(1, 5);
// Percentages with two decimals: "1.50%–3.25%"
IntlNumberRangeFormatter::createFromSkeleton(
'percent .00',
'en_GB',
IntlNumberRangeFormatter::COLLAPSE_NONE,
IntlNumberRangeFormatter::IDENTITY_FALLBACK_SINGLE_VALUE,
)->format(0.015, 0.0325);
An invalid skeleton throws an IntlException with U_NUMBER_SKELETON_SYNTAX_ERROR in the message, so skeletons belong in config or constants where they get exercised by a test, not interpolated from user input.
Choose a collapse mode#
Collapse controls how much of the two formatted numbers ICU is allowed to merge. COLLAPSE_NONE formats both ends in full. COLLAPSE_UNIT drops a repeated unit or currency affix. COLLAPSE_ALL merges everything it can, including shared digits in some locales. COLLAPSE_AUTO lets ICU decide per locale, which is the right default when you do not have a design constraint.
$modes = [
'NONE' => IntlNumberRangeFormatter::COLLAPSE_NONE,
'UNIT' => IntlNumberRangeFormatter::COLLAPSE_UNIT,
'ALL' => IntlNumberRangeFormatter::COLLAPSE_ALL,
'AUTO' => IntlNumberRangeFormatter::COLLAPSE_AUTO,
];
foreach ($modes as $label => $collapse) {
printf("%-5s %s\n", $label, IntlNumberRangeFormatter::createFromSkeleton(
'currency/GBP',
'en_GB',
$collapse,
IntlNumberRangeFormatter::IDENTITY_FALLBACK_APPROXIMATELY,
)->format(20, 50));
}
Broadly, NONE keeps both currency symbols and UNIT/ALL drop the second one, so you get something closer to £20–50. I am deliberately not pinning the exact separator here: the dash, the spacing around it and how aggressive AUTO is all come from CLDR data, and they differ between ICU versions. Run the loop above on your own build and snapshot what you get — that is the only output worth designing against.
Pick an identity fallback for equal endpoints#
Identity fallback is the knob nobody thinks about until a filter returns one product and the sidebar says £20–£20. It decides what happens when both ends format identically:
IDENTITY_FALLBACK_SINGLE_VALUE— render the value once:£20.IDENTITY_FALLBACK_APPROXIMATELY— render an approximation sign:~£20.IDENTITY_FALLBACK_APPROXIMATELY_OR_SINGLE_VALUE— approximate only when the inputs differ but round to the same string, otherwise a single value.IDENTITY_FALLBACK_RANGE— always render the full range, even when both ends match.
$single = IntlNumberRangeFormatter::createFromSkeleton(
'currency/GBP',
'en_GB',
IntlNumberRangeFormatter::COLLAPSE_AUTO,
IntlNumberRangeFormatter::IDENTITY_FALLBACK_SINGLE_VALUE,
);
$single->format(20, 20); // "£20"
$single->format(20.01, 20.02); // rounds to the same string, still collapses
For facets and filter chips I use SINGLE_VALUE. For anything that reports a measured interval — delivery estimates, latency bands — APPROXIMATELY_OR_SINGLE_VALUE is the honest choice, because it distinguishes "exactly 20" from "two different numbers that both print as 20".
Wrap the PHP intl number range formatter in a Laravel binding#
Constructing the formatter parses the skeleton and loads locale data. That is cheap once and noticeable five hundred times inside a product loop, the same shape of problem as any other per-row object allocation in a Blade view. Resolve it from the container, keyed by skeleton and locale.
namespace App\Providers;
use App\Support\NumberRangeFormatterFactory;
use Illuminate\Support\ServiceProvider;
class IntlServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->app->singleton(NumberRangeFormatterFactory::class);
}
}
namespace App\Support;
use IntlNumberRangeFormatter;
final class NumberRangeFormatterFactory
{
/** @var array<string, IntlNumberRangeFormatter> */
private array $cache = [];
public function __construct(
/** @var array<string, string> Laravel locale => ICU locale */
private readonly array $icuLocales = ['en' => 'en_GB', 'de' => 'de_DE', 'fr' => 'fr_FR'],
) {}
public function make(string $skeleton, ?string $locale = null): IntlNumberRangeFormatter
{
$locale ??= app()->getLocale();
$icuLocale = $this->icuLocales[$locale] ?? $locale;
$key = $skeleton.'|'.$icuLocale;
return $this->cache[$key] ??= IntlNumberRangeFormatter::createFromSkeleton(
$skeleton,
$icuLocale,
IntlNumberRangeFormatter::COLLAPSE_AUTO,
IntlNumberRangeFormatter::IDENTITY_FALLBACK_SINGLE_VALUE,
);
}
}
The locale map is the part people skip. Laravel's app()->getLocale() is usually a bare language tag like en, and ICU wants a region to know whether that means £ or $. Feeding en straight through gets you US formatting on a UK site.
Render the range from a Blade component#
With the factory in place the view layer gets one line. Make the component dumb: it takes two numbers and a skeleton, and knows nothing about ICU.
namespace App\View\Components;
use App\Support\NumberRangeFormatterFactory;
use Illuminate\View\Component;
class PriceRange extends Component
{
public function __construct(
public readonly float $from,
public readonly float $to,
public readonly string $skeleton = 'currency/GBP',
) {}
public function formatted(NumberRangeFormatterFactory $factory): string
{
return $factory->make($this->skeleton)->format($this->from, $this->to);
}
public function render(): string
{
return '<span class="tabular-nums">{{ $formatted(app(\App\Support\NumberRangeFormatterFactory::class)) }}</span>';
}
}
<x-price-range :from="$product->min_price" :to="$product->max_price" />
Ranges in a table want the digits to line up, which is a typography problem rather than an intl one — Tailwind's font-feature utilities for tabular numbers handle that part. If any of your locales are right-to-left, the surrounding layout needs logical properties rather than left/right utilities, because ICU will happily hand you a correctly ordered Arabic range and your CSS will put it in the wrong place.
Reverse user text with grapheme_strrev instead of strrev#
The other intl addition in 8.6 is grapheme_strrev(), and it exists because strrev() operates on bytes. Reverse a string containing a flag, a skin-toned emoji or a combining accent and you get mojibake, because the code points that make up one visible character end up in the wrong order. A multibyte-aware reversal is not enough either — it keeps each code point intact but still splits the cluster.
strrev('Apple🍏'); // mangled bytes
grapheme_strrev('Apple🍏'); // "🍏elppA"
grapheme_strrev('🇨🇳 - 🇳🇨'); // "🇳🇨 - 🇨🇳" — flags survive as units
grapheme_strrev('ABCDE'); // "EDCBA" — identical to strrev for ASCII
The signature is grapheme_strrev(string $string): string|false, declared in the global namespace, and it needs ext-intl like the rest of the grapheme family. This is the same class of bug as truncating a display name with substr(); if that sounds familiar, the multibyte string functions added in PHP 8.4 are the other half of the fix. On PHP 8.4 and 8.5 you can polyfill it in four lines with grapheme_str_split() and array_reverse().
Test the formatted output across locales#
Do not assert against hand-written strings you believe ICU produces. Assert the behaviour you actually care about — that two different locales produce different output, that equal endpoints collapse, and that the formatter is reused — and skip the test where ICU is too old.
<?php
use App\Support\NumberRangeFormatterFactory;
beforeEach(function () {
if (! extension_loaded('intl') || version_compare(INTL_ICU_VERSION, '63.0', '<')) {
$this->markTestSkipped('Requires ext-intl built against ICU 63+');
}
});
it('formats a currency range differently per locale', function () {
$factory = new NumberRangeFormatterFactory();
$gb = $factory->make('currency/GBP', 'en')->format(20, 50);
$de = $factory->make('currency/EUR', 'de')->format(20, 50);
expect($gb)->toContain('£')
->and($de)->not->toBe($gb);
});
it('collapses equal endpoints to a single value', function () {
$formatted = (new NumberRangeFormatterFactory())
->make('currency/GBP', 'en')
->format(20, 20);
expect($formatted)->not->toContain('–');
});
Pin the ICU version in your Docker image and CI runner. Two machines with different ICU builds will disagree about separators, and a test that asserts an exact string will fail on one of them for reasons that have nothing to do with your code.
Gotchas and Edge Cases#
The constructor is not usable. new IntlNumberRangeFormatter(...) throws an error telling you to use createFromSkeleton(). Static analysis will not catch it for you, so make the factory the only place the class name appears.
All four arguments to createFromSkeleton() are required. There is no "just give me the defaults" two-argument call, which is a reasonable design — collapse and identity fallback are exactly the decisions you should be forced to make.
Error state is global and historically sticky. PHP 8.6 shipped a fix so that createFromSkeleton() and format() reset the intl error state, but if you are reading intl_get_error_code() after a call, make sure you are on a patched 8.6 and not an early RC.
Output is CLDR data, not a contract. Separators, spacing and collapse aggressiveness change between ICU versions. Snapshot tests over exact range strings are a slow-motion CI failure.
Laravel locales are not ICU locales. en is not en_GB. Map them explicitly, and fail loudly on an unmapped locale rather than silently formatting dollars.
Wrapping Up#
Delete the range helper. Put the skeleton in config, pick a collapse mode and an identity fallback deliberately, resolve the formatter from the container, and let ICU own the string. Then grep for strrev( and decide, per call site, whether that string could ever contain something a user typed.
If you are working through the rest of 8.6, the new Time\Duration class is the other addition worth adopting early, and clamp() will delete more lines from your codebase than either of them.
FAQ#
How do I format a price range in PHP?
On PHP 8.6 and later, use IntlNumberRangeFormatter::createFromSkeleton() with a currency skeleton such as currency/GBP, then call format($low, $high). ICU picks the separator, the spacing and whether to repeat the currency symbol based on the locale you pass. Before 8.6 there was no stdlib answer, which is why nearly every codebase has a hand-rolled helper that only reads correctly in English.
What is IntlNumberRangeFormatter in PHP 8.6?
It is a new ext-intl class that formats an interval of two numbers as a single localised string, wrapping ICU's NumberRangeFormatter. You build it from an ICU number skeleton plus a locale, a collapse mode and an identity fallback, and it is supported as of ICU 63. It handles currencies, measurement units and percentages through the skeleton rather than through separate APIs.
How do I format a number range in Laravel for different locales?
Resolve the formatter from the container keyed by skeleton and locale, and derive the locale from app()->getLocale() through an explicit map to ICU locale identifiers. Laravel usually holds a bare language tag like en, while ICU needs a region to know whether that means pounds or dollars. Cache the formatter instance per key, because constructing one parses the skeleton and loads locale data.
Why does strrev break emoji, and what does grapheme_strrev do differently?
strrev() reverses a string byte by byte, so any character made of more than one byte comes back with its bytes in the wrong order. Even a code-point-aware reversal is insufficient, because a single visible character such as a flag or a skin-toned emoji is several code points joined together. grapheme_strrev(), added in PHP 8.6, reverses by grapheme cluster, keeping each visible character intact.
Do I need the intl extension for IntlNumberRangeFormatter?
Yes. Both IntlNumberRangeFormatter and grapheme_strrev() live in ext-intl, and the class additionally requires the extension to be built against ICU 63 or later. Guard with extension_loaded('intl') and a version_compare() against INTL_ICU_VERSION if your application has to run on hosts where intl might be missing, and keep your old concatenation helper as the fallback path.