Oihana PHP Standards

isLocale.php

Table of Contents

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.

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:

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).

Parameters
$tag : string

The locale tag to validate

$strict : bool = false

If true, cross-validate against ISO classes (default: false)

Tags
example
isLocale('fr-FR');                  // true
isLocale('zh-Hant-TW');             // true
isLocale('de-CH-1996');             // true
isLocale('en-x-pig-latin');         // true (private use)
isLocale('zz-ZZ');                  // true (syntax OK)
isLocale('zz-ZZ', strict: true);    // false (zz is not in ISO 639-1)
isLocale('fr-FR', strict: true);    // true
isLocale('');                       // false
link

BCP 47 / RFC 5646

author

Marc Alcaraz (ekameleon)

since
1.0.2
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
author

Marc Alcaraz (ekameleon)

since
1.0.2
Return values
array<string|int, mixed>|null

:?string,region:?string,variants:array<int,string>,privateUse:?string}|null

On this page

Search results