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

あなたは各桁をチェックしようとします:

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

$iしかし、その後、ループ内で減少します。

$i = $i - 1;

そのため、予想よりもループが少なくなります。

于 2012-04-22T08:32:24.680 に答える
1

以下の投稿があなたが探しているものを達成するのに役立つことを願っています:

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

于 2012-04-22T08:47:03.233 に答える
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 に答える