诸如如何使用带有 mysqli 的准备好的查询获取关联数组之类的问题已经得到修复?当它仅适用于mysqlnd
.
我不明白为什么在PHP
没有安装驱动程序或库的情况下创建不可用的功能会很复杂,假设如果安装了服务器,它apache
应该可以这样工作。
让我们看看以下用法需要具有Intl扩展才能使用:
- NumberFormatter::formatCurrency - 根据格式化程序规则格式化货币值。
例子:
$amount = '12345.67';
$formatter = new NumberFormatter('en_GB', NumberFormatter::CURRENCY);
echo 'UK: ', $formatter->formatCurrency($amount, 'EUR'), PHP_EOL;
$formatter = new NumberFormatter('de_DE', NumberFormatter::CURRENCY);
echo 'DE: ', $formatter->formatCurrency($amount, 'EUR'), PHP_EOL;
致命错误:未捕获的错误:在 C:\xampp\htdocs\Edition\formatodemoneda.php:4 中找不到类“NumberFormatter” 堆栈跟踪:#0 {main} 抛出
最后,money_format()
仅当系统具有容量时才定义功能strfmon
。例如,Windows
它没有,所以它money_format()
没有定义在Windows
.
例子
$number = 1234.56;
// let's print the international format for the en_US locale
setlocale(LC_MONETARY, 'en_US');
echo money_format('%i', $number) . "\n";
// USD 1,234.56
致命错误:未捕获错误:调用 C:\xampp\htdocs\Edition\formatodemoneda.php:21 中未定义的函数 money_format() 堆栈跟踪:#0 {main} 抛出
在 StackOcerflow English 上的一些类似问题中,观察到它试图为货币类型创建依赖函数,例如印度货币格式的函数,以及其他失败的尝试。但是,如果有多种货币,为每种货币添加这么多功能对我来说似乎不是一个解决方案。
直到我发现这个问题Alternative to money_format() Function in PHP on Windows Platform有一个非常奇怪的答案,它肯定可以用作创建解决方案的参考,mysqlnd
或者不得不放弃使用number-format
.
function money_format($floatcurr, $curr = 'EUR'){
$currencies['ARS'] = array(2, ',', '.'); // Argentine Peso
$currencies['AMD'] = array(2, '.', ','); // Armenian Dram
$currencies['AWG'] = array(2, '.', ','); // Aruban Guilder
$currencies['AUD'] = array(2, '.', ' '); // Australian Dollar
$currencies['BSD'] = array(2, '.', ','); // Bahamian Dollar
$currencies['BHD'] = array(3, '.', ','); // Bahraini Dinar
$currencies['BDT'] = array(2, '.', ','); // Bangladesh, Taka
$currencies['BZD'] = array(2, '.', ','); // Belize Dollar
$currencies['BMD'] = array(2, '.', ','); // Bermudian Dollar
$currencies['BOB'] = array(2, '.', ','); // Bolivia, Boliviano
$currencies['BAM'] = array(2, '.', ','); // Bosnia and Herzegovina, Convertible Marks
$currencies['BWP'] = array(2, '.', ','); // Botswana, Pula
$currencies['BRL'] = array(2, ',', '.'); // Brazilian Real
$currencies['BND'] = array(2, '.', ','); // Brunei Dollar
$currencies['CAD'] = array(2, '.', ','); // Canadian Dollar
$currencies['KYD'] = array(2, '.', ','); // Cayman Islands Dollar
$currencies['CLP'] = array(0, '', '.'); // Chilean Peso
$currencies['CNY'] = array(2, '.', ','); // China Yuan Renminbi
$currencies['COP'] = array(2, ',', '.'); // Colombian Peso
$currencies['CRC'] = array(2, ',', '.'); // Costa Rican Colon
$currencies['HRK'] = array(2, ',', '.'); // Croatian Kuna
$currencies['CUC'] = array(2, '.', ','); // Cuban Convertible Peso
$currencies['CUP'] = array(2, '.', ','); // Cuban Peso
$currencies['CYP'] = array(2, '.', ','); // Cyprus Pound
$currencies['CZK'] = array(2, '.', ','); // Czech Koruna
$currencies['DKK'] = array(2, ',', '.'); // Danish Krone
$currencies['DOP'] = array(2, '.', ','); // Dominican Peso
$currencies['XCD'] = array(2, '.', ','); // East Caribbean Dollar
$currencies['EGP'] = array(2, '.', ','); // Egyptian Pound
$currencies['SVC'] = array(2, '.', ','); // El Salvador Colon
$currencies['ATS'] = array(2, ',', '.'); // Euro
$currencies['BEF'] = array(2, ',', '.'); // Euro
$currencies['DEM'] = array(2, ',', '.'); // Euro
$currencies['EEK'] = array(2, ',', '.'); // Euro
$currencies['ESP'] = array(2, ',', '.'); // Euro
$currencies['EUR'] = array(2, ',', '.'); // Euro
$currencies['FIM'] = array(2, ',', '.'); // Euro
$currencies['FRF'] = array(2, ',', '.'); // Euro
$currencies['GRD'] = array(2, ',', '.'); // Euro
$currencies['IEP'] = array(2, ',', '.'); // Euro
$currencies['ITL'] = array(2, ',', '.'); // Euro
$currencies['LUF'] = array(2, ',', '.'); // Euro
$currencies['NLG'] = array(2, ',', '.'); // Euro
$currencies['PTE'] = array(2, ',', '.'); // Euro
$currencies['GHC'] = array(2, '.', ','); // Ghana, Cedi
$currencies['GIP'] = array(2, '.', ','); // Gibraltar Pound
$currencies['GTQ'] = array(2, '.', ','); // Guatemala, Quetzal
$currencies['HNL'] = array(2, '.', ','); // Honduras, Lempira
$currencies['HKD'] = array(2, '.', ','); // Hong Kong Dollar
$currencies['HUF'] = array(0, '', '.'); // Hungary, Forint
$currencies['ISK'] = array(0, '', '.'); // Iceland Krona
$currencies['INR'] = array(2, '.', ','); // Indian Rupee
$currencies['IDR'] = array(2, ',', '.'); // Indonesia, Rupiah
$currencies['IRR'] = array(2, '.', ','); // Iranian Rial
$currencies['JMD'] = array(2, '.', ','); // Jamaican Dollar
$currencies['JPY'] = array(0, '', ','); // Japan, Yen
$currencies['JOD'] = array(3, '.', ','); // Jordanian Dinar
$currencies['KES'] = array(2, '.', ','); // Kenyan Shilling
$currencies['KWD'] = array(3, '.', ','); // Kuwaiti Dinar
$currencies['LVL'] = array(2, '.', ','); // Latvian Lats
$currencies['LBP'] = array(0, '', ' '); // Lebanese Pound
$currencies['LTL'] = array(2, ',', ' '); // Lithuanian Litas
$currencies['MKD'] = array(2, '.', ','); // Macedonia, Denar
$currencies['MYR'] = array(2, '.', ','); // Malaysian Ringgit
$currencies['MTL'] = array(2, '.', ','); // Maltese Lira
$currencies['MUR'] = array(0, '', ','); // Mauritius Rupee
$currencies['MXN'] = array(2, '.', ','); // Mexican Peso
$currencies['MZM'] = array(2, ',', '.'); // Mozambique Metical
$currencies['NPR'] = array(2, '.', ','); // Nepalese Rupee
$currencies['ANG'] = array(2, '.', ','); // Netherlands Antillian Guilder
$currencies['ILS'] = array(2, '.', ','); // New Israeli Shekel
$currencies['TRY'] = array(2, '.', ','); // New Turkish Lira
$currencies['NZD'] = array(2, '.', ','); // New Zealand Dollar
$currencies['NOK'] = array(2, ',', '.'); // Norwegian Krone
$currencies['PKR'] = array(2, '.', ','); // Pakistan Rupee
$currencies['PEN'] = array(2, '.', ','); // Peru, Nuevo Sol
$currencies['UYU'] = array(2, ',', '.'); // Peso Uruguayo
$currencies['PHP'] = array(2, '.', ','); // Philippine Peso
$currencies['PLN'] = array(2, '.', ' '); // Poland, Zloty
$currencies['GBP'] = array(2, '.', ','); // Pound Sterling
$currencies['OMR'] = array(3, '.', ','); // Rial Omani
$currencies['RON'] = array(2, ',', '.'); // Romania, New Leu
$currencies['ROL'] = array(2, ',', '.'); // Romania, Old Leu
$currencies['RUB'] = array(2, ',', '.'); // Russian Ruble
$currencies['SAR'] = array(2, '.', ','); // Saudi Riyal
$currencies['SGD'] = array(2, '.', ','); // Singapore Dollar
$currencies['SKK'] = array(2, ',', ' '); // Slovak Koruna
$currencies['SIT'] = array(2, ',', '.'); // Slovenia, Tolar
$currencies['ZAR'] = array(2, '.', ' '); // South Africa, Rand
$currencies['KRW'] = array(0, '', ','); // South Korea, Won
$currencies['SZL'] = array(2, '.', ', '); // Swaziland, Lilangeni
$currencies['SEK'] = array(2, ',', '.'); // Swedish Krona
$currencies['CHF'] = array(2, '.', '\''); // Swiss Franc
$currencies['TZS'] = array(2, '.', ','); // Tanzanian Shilling
$currencies['THB'] = array(2, '.', ','); // Thailand, Baht
$currencies['TOP'] = array(2, '.', ','); // Tonga, Paanga
$currencies['AED'] = array(2, '.', ','); // UAE Dirham
$currencies['UAH'] = array(2, ',', ' '); // Ukraine, Hryvnia
$currencies['USD'] = array(2, '.', ','); // US Dollar
$currencies['VUV'] = array(0, '', ','); // Vanuatu, Vatu
$currencies['VEF'] = array(2, ',', '.'); // Venezuela Bolivares Fuertes
$currencies['VEB'] = array(2, ',', '.'); // Venezuela, Bolivar
$currencies['VND'] = array(0, '', '.'); // Viet Nam, Dong
$currencies['ZWD'] = array(2, '.', ' '); // Zimbabwe Dollar
// custom function to generate: ##,##,###.##
function formatinr($input)
{
$dec = "";
$pos = strpos($input, ".");
if ($pos === FALSE)
{
//no decimals
}
else
{
//decimals
$dec = substr(round(substr($input, $pos), 2), 1);
$input = substr($input, 0, $pos);
}
$num = substr($input, -3); // get the last 3 digits
$input = substr($input, 0, -3); // omit the last 3 digits already stored in $num
// loop the process - further get digits 2 by 2
while (strlen($input) > 0)
{
$num = substr($input, -2).",".$num;
$input = substr($input, 0, -2);
}
return $num.$dec;
}
if ($curr == "INR")
{
return formatinr($floatcurr);
}
else
{
return number_format($floatcurr, $currencies[$curr][0], $currencies[$curr][1], $currencies[$curr][2]);
}
}
告诉函数
number_format
输出哪种格式的简单方法是使用冒号运算符 (PHP 5.6+),称为可变长度参数列表或参数解包运算符(...),也动态使用bind_param($variable, ...$array);
。还有另一个函数称为
call_user_func_array()
用法示例:StackOverflow 上的其他参考PHP 中的三个点 (...) 是什么意思?.
最后我们得到了代码的一部分,我们告诉函数
number_format
使用变量生成什么样的硬币$curr
。另一种在变量中不定义
$curr = 'USD';
货币类型的使用方法是在我们的函数moneyFormat
中以这种方式进行:确实,首先要澄清为什么 PHP 需要其他库。
一般来说,一种编程语言包括一系列通用的基本函数和库,但其他的则来自第三方来源(并不总是与所有操作系统兼容) 。
在许多情况下,用纯 PHP 重新实现库而不是使用操作系统的预先存在的解决方案或功能(此外它不会是最佳的)是没有意义的,例如:
ffmpeg
处理视频的工具,能够使用功能并使用视频编辑服务器的操作系统必须安装此工具或管理 IMAP 邮件需要安装扩展程序php5-imap
。在许多情况下,在安装该语言的解释器或编译器时,它不会安装它认为不经常使用的包或库,因此另一种方法是检查文档以验证这是否不是编译选项:例如,
php_zip
为了能够使用它需要使用参数--enable-zip
https://www.php.net/manual/en/zip.installation.php编译 php对于您的具体问题,日期是技术问题中的一个有争议的话题,因为它们有很多表示形式并且取决于服务器的配置,我理解诸如
money_format
可以依靠使用操作系统的某些功能来咨询的方法这是服务器的语言环境。为什么 Apache 不为我解决这个问题?
Apache 仅作为接收请求的 Web 服务器与 PHP 交互,将其执行外包给 PHP 并向用户返回响应,在 PHP 7.x 版本中不需要 Apache,以免创建难以创建的依赖项Web 服务器和语言之间的区别在于它不提供补充库或依赖项,此外它会逃避它的角色。
那么我该如何处理 formatCurrency 呢?
目前,正如您评论的那样,有效的解决方案是:
auto_prepend_file
中