值得注意的是,此函式的預期行為可能會根據您的 ICU 版本而改變。
在 ICU 4.4.2 (Ubuntu 10.* 搭配 PHP 5.3.5 的標準)
使用地區設定 'en',輸入 100,1 會回傳 1001
在 ICU 4.8.1 (Ubuntu 12.* 搭配 PHP 5.3.10 的標準)
使用地區設定 'en',輸入 100,1 會回傳 "false"
請務必在 phpinfo() 中記下您的 ICU 版本,以確保您會得到預期的輸出。
(PHP 5 >= 5.3.0, PHP 7, PHP 8, PECL intl >= 1.0.0)
NumberFormatter::parse -- numfmt_parse — 解析數字
物件導向風格
$string
, 整數 $type
= NumberFormatter::TYPE_DOUBLE, 整數 &$offset
= null
): 整數|浮點數|false程序式風格
$formatter
,$string
,$type
= NumberFormatter::TYPE_DOUBLE,&$offset
= null
使用目前的格式器規則將字串解析為數字。
formatter
NumberFormatter 物件。
string
要解析數字的字串。
type
要使用的格式類型。預設使用 NumberFormatter::TYPE_DOUBLE
。請注意,NumberFormatter::TYPE_CURRENCY
不受支援;請改用 NumberFormatter::parseCurrency()。
offset
開始解析的字串中的偏移量。返回時,此值將保存解析結束的偏移量。
已解析數字的值,或錯誤時返回 false
。
範例 #1 numfmt_parse() 範例
<?php
$fmt = numfmt_create( 'de_DE', NumberFormatter::DECIMAL );
$num = "1.234.567,891";
echo numfmt_parse($fmt, $num)."\n";
echo numfmt_parse($fmt, $num, NumberFormatter::TYPE_INT32)."\n";
?>
範例 #2 物件導向範例
<?php
$fmt = new NumberFormatter( 'de_DE', NumberFormatter::DECIMAL );
$num = "1.234.567,891";
echo $fmt->parse($num)."\n";
echo $fmt->parse($num, NumberFormatter::TYPE_INT32)."\n";
?>
上述範例會輸出:
1234567.891 1234567
值得注意的是,此函式的預期行為可能會根據您的 ICU 版本而改變。
在 ICU 4.4.2 (Ubuntu 10.* 搭配 PHP 5.3.5 的標準)
使用地區設定 'en',輸入 100,1 會回傳 1001
在 ICU 4.8.1 (Ubuntu 12.* 搭配 PHP 5.3.10 的標準)
使用地區設定 'en',輸入 100,1 會回傳 "false"
請務必在 phpinfo() 中記下您的 ICU 版本,以確保您會得到預期的輸出。
'en_EN'
基本上第一部分是語言,第二部分是地區
'en_EN' - 英文,英國
'en_US' - 英文,美國
您可以在這裡查詢語言標籤,例如 'en_EN'
https://datahub.io/core/language-codes
請參閱 "ietf-language-tags"