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

Measurements from localized strings

When a string is entered in a localized notation, the correct interpretation can not be determined without knowing the intended locale. The division of decimal digits with "." and grouping of thousands with "," is common in the English language, but not so in other languages. For example, the English number "1,234.50" would be interpreted as meaning "1.2345" in German. To deal with such problems, the locale-aware Zend_Measure_* family of classes offer the possibility to specify a language or region to disambiguate the input data and properly interpret the intended semantic value.

Example 610. Localized string

<?php
$locale 
= new Zend_Locale('de');
$mystring "1,234.50";
$unit = new Zend_Measure_Length($mystring,
                                
Zend_Measure_Length::STANDARD,
                                
$locale);
echo 
$unit// outputs "1.234 m"

$mystring "1,234.50";
$unit = new Zend_Measure_Length($mystring,
                                
Zend_Measure_Length::STANDARD,
                                
'en_US');
echo 
$unit// outputs "1234.50 m"

Since Zend Framework 1.7.0 Zend_Measure does also support the usage of an application wide locale. You can simply set a Zend_Locale instance to the registry like shown below. With this notation you can forget about setting the locale manually with each instance when you want to use the same locale multiple times.

<?php
// in your bootstrap file
$locale = new Zend_Locale('de_AT');
Zend_Registry::set('Zend_Locale'$locale);

// somewhere in your application
$length = new Zend_Measure_Length(Zend_Measure_Length::METER();

Zend Framework