PHP bir kelime bir sayı dönüştürmek için kolay bir yol var mı?

7 Cevap php

PHP, bir kelime bir sayı dönüştürmek için kolay bir yol var mı? Örneğin, 27 ile twenty-seven.

7 Cevap

I found, bazı (2007/2008) kaynak kodu çevrimiçi ve telif hakkı ama ben özgürce kullanmak ve değiştirebilirsiniz ancak ben istiyorum, bu yüzden CC-Wiki altında burada ve yeniden lisans yerleştirin:

<?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";
    }
}

Bunun için iki bağlantılar bulundu. first siz kullanabilirsiniz ücretsiz bir PHP sınıfı, second sen de bir göz ve kopyalamak veya kendi yazmak için kullanabileceğiniz bir örnek fonksiyonudur.

Orada ben farkında değilim hiçbir yerleşik bir yolu, ama benim örnek kod bir göz varsa this question (C #) bu çalışma almak için yeterince kolay olmalıdır. Temelde sadece değişkenleri untyping ve çalışması gerekir uygun ve $ önek ekleyerek meselesi olurdu.

Ben standart ABD yazılı kelime sayı biçimini takmak için yukarıda kodu yazdım.

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));
        }
    }
}

Ben, bir özyinelemeli işlevini kullanarak bu yarattı.

$wordnum = numberToWord($number);
echo $wordnum."<BR>";

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:$word = "Hundred";break;
            case 4:$word = "Thousand";break;
            case 5:$word = "Thousand";break;
            case 6:$word = "Lakh";break;
            case 7:$word = "Lakh";break;
            case 8:$word = "Crore";break;
            case 9:$word = "Crore";break;

        }
        return $word;
    }

    function numberToWord($number){

        $numberlength = strlen($number);
        if ($numberlength == 1) { 
            return singledigit($number);
        }elseif ($numberlength == 2) {
            return doubledigit($number);
        }
        else {

            $word = "";
            $wordin = "";

            if($numberlength == 9){
                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));
                }
            }

            if($numberlength == 7){
                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));
                }

            }

            if($numberlength == 5){
                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));
                }


            }
            else{
                if($number[0] >0){
                    $unitdigit = unitdigit($numberlength,$number[0]);
                    $word = singledigit($number[0]) ." ".$unitdigit." ";
                }               
                return $word." ".numberToWord(substr($number,1));
            }
        }
    }