PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Numeral System Conversion

Zend_Locale_Format::convertNumerals() converts digits between different numeral systems, including the standard Arabic/European/Latin numeral system (0,1,2,3,4,5,6,7,8,9), not to be confused with Eastern Arabic numerals sometimes used with the Arabic language to express numerals. Attempts to use an unsupported numeral system will result in an exception, to avoid accidentally performing an incorrect conversion due to a spelling error. All characters in the input, which are not numerals for the selected numeral system, are copied to the output with no conversion provided for unit separator characters. Zend_Locale* components rely on the data provided by CLDR (see their list of scripts grouped by language).

In CLDR and hereafter, the Europena/Latin numerals will be referred to as "Latin" or by the assigned 4-letter code "Latn". Also, the CLDR refers to this numeral systems as "scripts".

Suppose a web form collected a numeric input expressed using Eastern Arabic digits "١‎٠٠". Most software and PHP functions expect input using Arabic numerals. Fortunately, converting this input to its equivalent Latin numerals "100" requires little effort using convertNumerals($inputNumeralString, $sourceNumeralSystem, $destNumeralSystem), which returns the $input with numerals in the script $sourceNumeralSystem converted to the script $destNumeralSystem.

Example 577. Converting numerals from Eastern Arabic scripts to European/Latin scripts

<?php
$arabicScript 
"١‎٠٠";   // Arabic for "100" (one hundred)
$latinScript Zend_Locale_Format::convertNumerals($arabicScript,
                                                   
'Arab',
                                                   
'Latn');

print 
"\nOriginal:   " $arabicScript;
print 
"\nNormalized: " $latinScript;

Similarly, any of the supported numeral systems may be converted to any other supported numeral system.

Example 578. Converting numerals from Latin script to Eastern Arabic script

<?php
$latinScript 
'123';
$arabicScript Zend_Locale_Format::convertNumerals($latinScript,
                                                    
'Latn',
                                                    
'Arab');

print 
"\nOriginal:  " $latinScript;
print 
"\nLocalized: " $arabicScript;

Example 579.  Getting 4 letter CLDR script code using a native-language name of the script

<?php
function getScriptCode($scriptName$locale)
{
    
$scripts2names Zend_Locale_Data::getList($locale'script');
    
$names2scripts array_flip($scripts2names);
    return 
$names2scripts[$scriptName];
}
echo 
getScriptCode('Latin''en'); // outputs "Latn"
echo getScriptCode('Tamil''en'); // outputs "Taml"
echo getScriptCode('tamoul''fr'); // outputs "Taml"

For a list of supported numeral systems call Zend_Locale::getTranslationList('numberingsystem', 'en').

Zend Framework