0

我有这个 php 代码:

<?php
$numarray = trim($_GET['num']);
$i = strlen($numarray);
$result = "";
$numneedarray = array(
90 => 'ninety',
80 => 'eighty',
70 => 'seventy',
60 => 'sixty',
50 => 'fifty',
40 => 'forty',
30 => 'thirty',
20 => 'twenty',
19 => 'nineteen',
18 => 'eighteen',
17 => 'seventeen',
16 => 'sixteen',
15 => 'fifteen',
14 => 'fourteen',
13 => 'thirteen',
12 => 'twelve',
11 => 'eleven',
10 => 'ten',
9 => 'nine',
8 => 'eight',
7 => 'seven',
6 => 'six',
5 => 'five',
4 => 'four',
3 => 'three',
2 => 'two',
1 => 'one'
);
for ($v = 1; $v <= $i; $v++) {
if ($i > 10) {
exit("Has to be 10-digit or less.");
}
if ($i == 3) {
    $result .= ", " . $numneedarray[$numarray[strlen($numarray) - 3]] . " hundred";
    if ($numarray % 100 == 0) {
    echo "Hi95";
    break;
    }
} elseif ($i == 2) {
    if ($numarray[$v] == 1) {
        $othernum = $numarray[strlen($numarray) - 1];
        $othernum2 = $numarray[strlen($numarray) - 2] * 10;
        $othernum3 = $othernum2 + $othernum;
        if (strlen($numarray) > 2) {
        $result .= " and ";
        }
        $result .= $numneedarray[$othernum3];
        echo "Hi107";
    } else {
        $othernum = $numarray[strlen($numarray) - 2] * 10;
        $othernum2 = $numarray[strlen($numarray) - 1];
        $result .= " and " . $numneedarray[$othernum] . " " . $numneedarray[$othernum2];
        echo "Hi112";
    }
}
if ($i == 10) {
    $digit = substr($numarray, 0, 1);
    $result .= $numneedarray[$digit] . " billion";
    if ($numarray % 1000000000 == 0) {
    break;
    }
} elseif ($i == 9) {
    $number = substr($numarray, 1, 3);
    $digit1 = substr($number, 0, 1);
    $digit1con = $numneedarray[$digit1] . " hundred";
    $digit2 = substr($number, 1, 1);
    $noneed = false;
    if ($digit2 != 1) {
    $digit2con = $numneedarray[$digit2 * 10];
    } else {
    $digit23 = substr($number, 1, 2);
    $digit23con = $numneedarray[$digit23];
    $noneed = true;
    }
        $digit3 = substr($number, -1);
        $digit3con = $numneedarray[$digit3];
    if ($noneed == true) {
        $result .= ", " . $digit1con . " and " . $digit23con . " million";
    } else {
        $result .= ", " . $digit1con . " and " . $digit2con . " " . $digit3con . " million";
    }
    if ($numarray % 100000000 == 0) {
    echo "Hi";
    break;
    }
} elseif ($i == 6) {
    $number = substr($numarray, 4, 3);
    $digit1 = substr($number, 0, 1);
    $digit1con = $numneedarray[$digit1] . " hundred";
    $digit2 = substr($number, 1, 1);
    $noneed = false;
    if ($digit2 != 1) {
    $digit2con = $numneedarray[$digit2 * 10];
    } else {
    $digit23 = substr($number, 1, 2);
    $digit23con = $numneedarray[$digit23];
    $noneed = true;
    }
        $digit3 = substr($number, -1);
        $digit3con = $numneedarray[$digit3];
    if ($noneed == true) {
        $result .= ", " . $digit1con . " and " . $digit23con . " thousand";
    } else {
        $result .= ", " . $digit1con . " and " . $digit2con . " " . $digit3con . " thousand";
    }
    if ($numarray % 100000 == 0) {
    echo "Hi89";
    break;
    }
} 
echo $i;
$i = $i - 1;
}
if (strlen($numarray) == 1) {
    echo $numneedarray[$numarray];
}
echo $result;
?>

num值等于1234567890。当我刷新页面时, 的值$i只会从10 -> 9 -> 8 -> 7 -> 6然后突然停止。为什么循环会停止运行?

4

3 回答 3

1

我希望下面的帖子能帮助你实现你正在寻找的东西:

http://www.karlrixon.co.uk/writing/convert-numbers-to-words-with-php/

于 2012-04-22T08:47:03.233 回答
1

您尝试检查每个数字:

for ($v = 1; $v <= $i; $v++) {

但随后你$i在循环中减少:

$i = $i - 1;

这就是为什么您看到的循环比您预期的要少。

于 2012-04-22T08:32:24.680 回答
0

不是最优的 - 但改变

  $i = strlen($numarray);

 $i = strlen($numarray);
 $count = $i;

 for ($v = 1; $v <= $i; $v++)

  for ($v = 1; $v <= $count; $v++)
于 2012-04-22T08:33:56.143 回答