PHPでは、数字を単語に変換する簡単な方法はありますか?たとえば、27 to twenty-seven。
I found some(2007/2008)オンラインのソースコードであり、著作権であるため、自由に使用して自由に変更できます。ここに配置し、CC-Wikiで再ライセンスします。 :
<?php
/**
* English Number Converter - Collection of PHP functions to convert a number
* into English text.
*
* This exact code is licensed under CC-Wiki on Stackoverflow.
* http://creativecommons.org/licenses/by-sa/3.0/
*
* @link http://stackoverflow.com/q/277569/367456
* @question Is there an easy way to convert a number to a Word in PHP?
*
* This file incorporates work covered by the following copyright and
* permission notice:
*
* Copyright 2007-2008 Brenton Fletcher. http://bloople.net/num2text
* You can use this freely and modify it however you want.
*/
function convertNumber($number)
{
list($integer, $fraction) = explode(".", (string) $number);
$output = "";
if ($integer{0} == "-")
{
$output = "negative ";
$integer = ltrim($integer, "-");
}
else if ($integer{0} == "+")
{
$output = "positive ";
$integer = ltrim($integer, "+");
}
if ($integer{0} == "0")
{
$output .= "zero";
}
else
{
$integer = str_pad($integer, 36, "0", STR_PAD_LEFT);
$group = rtrim(chunk_split($integer, 3, " "), " ");
$groups = explode(" ", $group);
$groups2 = array();
foreach ($groups as $g)
{
$groups2[] = convertThreeDigit($g{0}, $g{1}, $g{2});
}
for ($z = 0; $z < count($groups2); $z++)
{
if ($groups2[$z] != "")
{
$output .= $groups2[$z] . convertGroup(11 - $z) . (
$z < 11
&& !array_search('', array_slice($groups2, $z + 1, -1))
&& $groups2[11] != ''
&& $groups[11]{0} == '0'
? " and "
: ", "
);
}
}
$output = rtrim($output, ", ");
}
if ($fraction > 0)
{
$output .= " point";
for ($i = 0; $i < strlen($fraction); $i++)
{
$output .= " " . convertDigit($fraction{$i});
}
}
return $output;
}
function convertGroup($index)
{
switch ($index)
{
case 11:
return " decillion";
case 10:
return " nonillion";
case 9:
return " octillion";
case 8:
return " septillion";
case 7:
return " sextillion";
case 6:
return " quintrillion";
case 5:
return " quadrillion";
case 4:
return " trillion";
case 3:
return " billion";
case 2:
return " million";
case 1:
return " thousand";
case 0:
return "";
}
}
function convertThreeDigit($digit1, $digit2, $digit3)
{
$buffer = "";
if ($digit1 == "0" && $digit2 == "0" && $digit3 == "0")
{
return "";
}
if ($digit1 != "0")
{
$buffer .= convertDigit($digit1) . " hundred";
if ($digit2 != "0" || $digit3 != "0")
{
$buffer .= " and ";
}
}
if ($digit2 != "0")
{
$buffer .= convertTwoDigit($digit2, $digit3);
}
else if ($digit3 != "0")
{
$buffer .= convertDigit($digit3);
}
return $buffer;
}
function convertTwoDigit($digit1, $digit2)
{
if ($digit2 == "0")
{
switch ($digit1)
{
case "1":
return "ten";
case "2":
return "twenty";
case "3":
return "thirty";
case "4":
return "forty";
case "5":
return "fifty";
case "6":
return "sixty";
case "7":
return "seventy";
case "8":
return "eighty";
case "9":
return "ninety";
}
} else if ($digit1 == "1")
{
switch ($digit2)
{
case "1":
return "eleven";
case "2":
return "twelve";
case "3":
return "thirteen";
case "4":
return "fourteen";
case "5":
return "fifteen";
case "6":
return "sixteen";
case "7":
return "seventeen";
case "8":
return "eighteen";
case "9":
return "nineteen";
}
} else
{
$temp = convertDigit($digit2);
switch ($digit1)
{
case "2":
return "twenty-$temp";
case "3":
return "thirty-$temp";
case "4":
return "forty-$temp";
case "5":
return "fifty-$temp";
case "6":
return "sixty-$temp";
case "7":
return "seventy-$temp";
case "8":
return "eighty-$temp";
case "9":
return "ninety-$temp";
}
}
}
function convertDigit($digit)
{
switch ($digit)
{
case "0":
return "zero";
case "1":
return "one";
case "2":
return "two";
case "3":
return "three";
case "4":
return "four";
case "5":
return "five";
case "6":
return "six";
case "7":
return "seven";
case "8":
return "eight";
case "9":
return "nine";
}
}
または、PHP)の intl
パッケージのNumberFormatterクラスを使用できます。サンプルコードを次に示します。開始するには(コマンドライン用):
<?php
if ($argc < 3)
{
echo "usage: php {$argv[0]} lang-tag number ...\n";
exit;
}
array_shift($argv);
$lang_tag = array_shift($argv);
$nf1 = new NumberFormatter($lang_tag, NumberFormatter::DECIMAL);
$nf2 = new NumberFormatter($lang_tag, NumberFormatter::SPELLOUT);
foreach ($argv as $num)
{
echo $nf1->format($num).' is '.$nf2->format($num)."\n";
}
そこには - Numbers_Words
package PECLで。まさにあなたが求めていることをします。次の言語がサポートされています。
上記のコードを書き直して、米国で書かれた標準のWord数値形式に合わせました。
function singledigit($number){
switch($number){
case 0:$Word = "zero";break;
case 1:$Word = "one";break;
case 2:$Word = "two";break;
case 3:$Word = "three";break;
case 4:$Word = "four";break;
case 5:$Word = "five";break;
case 6:$Word = "six";break;
case 7:$Word = "seven";break;
case 8:$Word = "eight";break;
case 9:$Word = "nine";break;
}
return $Word;
}
function doubledigitnumber($number){
if($number == 0){
$Word = "";
}
else{
$Word = "-".singledigit($number);
}
return $Word;
}
function doubledigit($number){
switch($number[0]){
case 0:$Word = doubledigitnumber($number[1]);break;
case 1:
switch($number[1]){
case 0:$Word = "ten";break;
case 1:$Word = "eleven";break;
case 2:$Word = "twelve";break;
case 3:$Word = "thirteen";break;
case 4:$Word = "fourteen";break;
case 5:$Word = "fifteen";break;
case 6:$Word = "sixteen";break;
case 7:$Word = "seventeen";break;
case 8:$Word = "eighteen";break;
case 9:$Word = "ninteen";break;
}break;
case 2:$Word = "twenty".doubledigitnumber($number[1]);break;
case 3:$Word = "thirty".doubledigitnumber($number[1]);break;
case 4:$Word = "forty".doubledigitnumber($number[1]);break;
case 5:$Word = "fifty".doubledigitnumber($number[1]);break;
case 6:$Word = "sixty".doubledigitnumber($number[1]);break;
case 7:$Word = "seventy".doubledigitnumber($number[1]);break;
case 8:$Word = "eighty".doubledigitnumber($number[1]);break;
case 9:$Word = "ninety".doubledigitnumber($number[1]);break;
}
return $Word;
}
function unitdigit($numberlen,$number){
switch($numberlen){
case 3:case 6:case 9:case 12:$Word = "hundred";break;
case 4:case 5:$Word = "thousand";break;
case 7:case 8:$Word = "million";break;
case 10:case 11:$Word = "billion";break;
}
return $Word;
}
function numberToWord($number){
$numberlength = strlen($number);
if ($numberlength == 1) {
return singledigit($number);
}elseif ($numberlength == 2) {
return doubledigit($number);
}
else {
$Word = "";
$wordin = "";
switch ($numberlength ) {
case 5:case 8: case 11:
if($number[0] >0){
$unitdigit = unitdigit($numberlength,$number[0]);
$Word = doubledigit($number[0].$number[1]) ." ".$unitdigit." ";
return $Word." ".numberToWord(substr($number,2));
}
else{
return $Word." ".numberToWord(substr($number,1));
}
break;
default:
if($number[0] >0){
$unitdigit = unitdigit($numberlength,$number[0]);
$Word = singledigit($number[0]) ." ".$unitdigit." ";
}
return $Word." ".numberToWord(substr($number,1));
}
}
}
返される文字列に「and」を入れて、通常は人間が言うように文にフォーマットするソリューションが必要でした。だから私は少し違うソリューションを適応させた-これは誰かに役立つと思ったので投稿した。
4,835,301 returns "Four million eight hundred and thirty five thousand three hundred and one."
コード
function convertNumber($num = false)
{
$num = str_replace(array(',', ''), '' , trim($num));
if(! $num) {
return false;
}
$num = (int) $num;
$words = array();
$list1 = array('', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven',
'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'
);
$list2 = array('', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety', 'hundred');
$list3 = array('', 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion', 'septillion',
'octillion', 'nonillion', 'decillion', 'undecillion', 'duodecillion', 'tredecillion', 'quattuordecillion',
'quindecillion', 'sexdecillion', 'septendecillion', 'octodecillion', 'novemdecillion', 'vigintillion'
);
$num_length = strlen($num);
$levels = (int) (($num_length + 2) / 3);
$max_length = $levels * 3;
$num = substr('00' . $num, -$max_length);
$num_levels = str_split($num, 3);
for ($i = 0; $i < count($num_levels); $i++) {
$levels--;
$hundreds = (int) ($num_levels[$i] / 100);
$hundreds = ($hundreds ? ' ' . $list1[$hundreds] . ' hundred' . ( $hundreds == 1 ? '' : '' ) . ' ' : '');
$tens = (int) ($num_levels[$i] % 100);
$singles = '';
if ( $tens < 20 ) {
$tens = ($tens ? ' and ' . $list1[$tens] . ' ' : '' );
} elseif ($tens >= 20) {
$tens = (int)($tens / 10);
$tens = ' and ' . $list2[$tens] . ' ';
$singles = (int) ($num_levels[$i] % 10);
$singles = ' ' . $list1[$singles] . ' ';
}
$words[] = $hundreds . $tens . $singles . ( ( $levels && ( int ) ( $num_levels[$i] ) ) ? ' ' . $list3[$levels] . ' ' : '' );
} //end for loop
$commas = count($words);
if ($commas > 1) {
$commas = $commas - 1;
}
$words = implode(' ', $words);
$words = preg_replace('/^\s\b(and)/', '', $words );
$words = trim($words);
$words = ucfirst($words);
$words = $words . ".";
return $words;
}
NumberFormatterクラスを使用すると、単語に簡単に変換できます。
<?php
$number = '12345';
$locale = 'en_US';
$fmt = numfmt_create($locale, NumberFormatter::SPELLOUT);
$in_words = numfmt_format($fmt, $number);
print_r($in_words);
// twelve thousand three hundred forty-five
?>
NumberFormatterクラス を使用できます。
$f = new NumberFormatter("en", NumberFormatter::SPELLOUT);
echo $f->format($myNumber);
はいあります。ライブラリを使用せずに、これに従うだけです。
最初に、;extension=php_intl.dll
はphp.iniで有効になっていますが、まだ動作しない場合は、この回答を確認する必要があります。
正常に移動した後、すべてのファイルはicuで始まります。
from:<wamp_installation_path>/bin/php/php5.4.3/
宛先:<wamp_installation_path>/bin/Apache/apache2.2.22/bin/
サーバーを再起動します。
このコードを実行してみてください:
$f = new NumberFormatter("en", NumberFormatter::SPELLOUT);
echo $f->format(123456);
百二三四千四百五十六の出力を表示します。
それがみんなに役立つことを願っています:)。