将数字或整数转换为文字

3
我希望把整数转换成单词,例如:29会变成TWENTY NINE,50会变成FIFTY。我该如何使用PHP实现这个功能?以下是我的代码,但它没有达到期望的输出结果。
$fees_so = $form_data_fees['field']['4'];
$feesInWords = strval($fees_so);
echo $feesInWords;

http://php.net/manual/zh/function.strval.php - 该函数返回的值不进行任何格式化。如果您想将数字格式化为字符串,请参阅sprintf()或number_format()。 - ggdx
你调试过$form_data_fees['field']['4']吗? $form_data_fees['field']包含什么?请在你的问题中添加该数组的数据示例。 - Philipp Maurer
1
请查看这个stackoverflow链接 https://stackoverflow.com/questions/17633985/converting-numbers-to-words-in-php 。它应该能解决你的问题。我刚测试过了... - Lalbhusan Yadav
$fees_so变量返回£29,这是从php表单中选择的。 - sc2015
@LalbhusanYadav 有比那更好的方法。 - user7780894
3个回答

6

您可以使用NumberFormatter类,并使用SPELLOUT

$nf = new NumberFormatter("en", NumberFormatter::SPELLOUT);
echo $nf->format(1999); // one thousand nine hundred ninety-nine

2

最终我使用@aniket-sahrawat的代码并进行了一些调整,找到了解决方案。

以下是代码,如果有人将来需要,请参考...

<?php
$fees_so = $form_data_fees['field']['4'];
$words = filter_var($fees_so, FILTER_SANITIZE_NUMBER_INT);
$nf = new NumberFormatter("en", NumberFormatter::SPELLOUT);
echo $nf->format($words);
?>

1
假设您有一个数组:
$list_of_fees = array("£29", "£50", "£64");;
for($i = 0; $i < 3; $i++)
    echo substr($list_of_fees[$i], 1) . " Pounds";

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接