natsort, Collator and the Spaceship: PHP Sorting Like a Human
"item10" before "item2", "Öl" after "z", uppercase clustering before lowercase - default sorts betray human expectations three different ways. PHP has a fix for each.
Step-by-step programming tutorials on PHP, JavaScript, Laravel, WordPress and more.
"item10" before "item2", "Öl" after "z", uppercase clustering before lowercase - default sorts betray human expectations three different ways. PHP has a fix for each.
Currency placement, month names, ordinal suffixes, "1.234,56" vs "1,234.56" - the intl extension knows every locale's rules, and most PHP developers never call it.
String length counts UTF-16 code units, not characters - so emoji truncation splits families into corpses. Intl.Segmenter counts graphemes, words and sentences correctly.
Intl.PluralRules knows every language's plural categories and Intl.ListFormat writes "a, b, and c" correctly - two tiny APIs that end string-building guesswork.
Plain sort puts item10 before item2 because strings compare character by character. Intl.Collator fixes it, handles accents and case, and is faster than localeCompare in loops.
Intl.NumberFormat handles currencies, compact notation, percentages and units in the user's locale - the kind of formatting everyone installs a package for.
You do not need moment.js or dayjs to print "yesterday" or "in 3 weeks". The Intl API does relative time, in the user's own language, with zero dependencies.