Search code examples
phplocale

Convert locale long format to short one


I have following locale strings:

pl_PL
en_GB

Is it possible to convert it with some native function that works with real locale dataset into:

pl
en

Solutions like:

list($short) = explode('_', 'pl_PL');

is not expected right now.


Solution

  • locale_parse from the intl extension should do this for you. From the documentation:

    <?php
    $arr = locale_parse('sl-Latn-IT-nedis');
    if ($arr) {
        foreach ($arr as $key => $value) {
            echo "$key : $value , ";
            // language : sl , script : Latn , region : IT , variant0 : NEDIS ,
        }
    }
    ?>