isLocale.php
Table of Contents
Constants
- BCP47_ONLY_LANGUAGES : mixed = ['bh', 'sh']
- Two-letter language subtags the IANA registry declares but ISO 639-1 does not list.
Functions
- isLocale() : bool
- Validates whether a string is a valid IETF BCP 47 / RFC 5646 locale tag.
- parseLocaleTag() : array<string|int, mixed>|null
- Parses a BCP 47 locale tag into its canonical components.
Constants
BCP47_ONLY_LANGUAGES
Two-letter language subtags the IANA registry declares but ISO 639-1 does not list.
public
mixed
BCP47_ONLY_LANGUAGES
= ['bh', 'sh']
bh is a collection (Bihari languages) and sh a macrolanguage (Serbo-Croatian).
Neither is deprecated, so both are valid BCP 47 language subtags.
The five deprecated forms — in, iw, ji, jw, mo — are deliberately absent.
Functions
isLocale()
Validates whether a string is a valid IETF BCP 47 / RFC 5646 locale tag.
isLocale(string $tag[, bool $strict = false ]) : bool
Recognized minimal grammar:
language[-script][-region][-variant...][-x-private...]
- language: 2 or 3 letters (e.g.
fr,en,und) - script: 4 letters (e.g.
Latn,Hant) - region: 2 letters (ISO 3166-1) or 3 digits (UN M49)
- variant: 5–8 alphanumerics, or a digit followed by 3 alphanumerics (e.g.
1996,scotland) - private:
x-followed by one or more 1–8 alphanumeric subtags
Case is normalized (subtags are case-insensitive in BCP 47). Extlangs,
Unicode/transformation extensions (-u-, -t-) and grandfathered tags are
intentionally not supported in this minimal implementation.
When $strict is true, the parsed components are cross-validated against
the existing standards classes:
language(2 letters) → ISO639_1script→ ISO15924region(2 letters) → ISO3166_1region(3 digits) → UNM49Numeric
3-letter languages are not cross-validated (ISO 639-2/3 classes are not
available yet — see the future org\iso\ISO639_2 and org\iso\ISO639_5).
BCP 47 takes its authority from the IANA Language Subtag Registry, which is a
superset of ISO 639-1 : it keeps collections and macrolanguages that ISO 639-1
does not list. Two of them, bh and sh, are valid and not deprecated, so
validating against ISO639_1 alone would reject them. They are accepted
through BCP47_ONLY_LANGUAGES. The five deprecated forms — in, iw,
ji, jw, mo — stay rejected, which is what their deprecation calls for.
Parameters
- $tag : string
-
The locale tag to validate
- $strict : bool = false
-
If true, cross-validate against ISO classes (default: false)
Tags
Return values
bool —True if the tag is a valid BCP 47 locale, false otherwise
parseLocaleTag()
Parses a BCP 47 locale tag into its canonical components.
parseLocaleTag(string $tag) : array<string|int, mixed>|null
Returns an associative array with keys language, script, region,
variants (array), privateUse (full x-... string or null). Returns
null if the input does not match the supported grammar.
Subtags are normalized to canonical case:
- language → lowercase
- script → Titlecase
- region → uppercase
- variants → lowercase
Parameters
- $tag : string
-
The locale tag to parse
Tags
Return values
array<string|int, mixed>|null —:?string,region:?string,variants:array<int,string>,privateUse:?string}|null