Home > Article > Backend Development > How to use PHP's Intl extension?
PHP's Intl extension is a very practical tool that provides a series of internationalization and localization functions. This article will introduce how to use PHP's Intl extension.
1. Install the Intl extension
Before you start using the Intl extension, you need to install the extension. Under Windows, the extension can be opened in the php.ini file. Under Linux, it can be installed via the command line:
Ubuntu/Debian:
sudo apt-get install php7.4-intl
RedHat/Fedora:
sudo yum install php-intl
After the installation is complete, you can confirm whether the extension has been installed by running the following command:
php -m | grep intl
2. Use Intl extension
Intl extension provides many useful functions, the following are some commonly used functions:
Use Locale:: getDefault() can obtain the current system default locale:
echo Locale::getDefault(); // Output: en_US
Use the Collator class for string comparison:
$collator = new Collator('zh_CN');
echo $collator->compare('China', 'United States'); // Output : 1
Use the NumberFormatter class for number formatting:
$formatter = new NumberFormatter('zh_CN', NumberFormatter:: DECIMAL);
echo $formatter->format(12345.67); // Output: 12,345.67
Use the NumberFormatter class for currency formatting :
$formatter = new NumberFormatter('zh_CN', NumberFormatter::CURRENCY);
echo $formatter->formatCurrency(1234.56, 'CNY'); // Output: CNY1,234.56
Use the IntlDateFormatter class for date formatting:
$formatter = new IntlDateFormatter(
'zh_CN', IntlDateFormatter::LONG, IntlDateFormatter::LONG, 'Asia/Shanghai', IntlDateFormatter::GREGORIAN
);
echo $formatter->format(new DateTime()); // Output: Monday, June 7, 2021 GMT 8 12:00:00 AM
The above are some commonly used functions and classes. If you want to know more about the use of functions and classes, please refer to the official documentation.
3. Summary
Using Intl extension can easily perform internationalization and localization operations. This article introduces how to install the extension and some commonly used functions and classes. I hope it will be helpful to you.
The above is the detailed content of How to use PHP's Intl extension?. For more information, please follow other related articles on the PHP Chinese website!