devnagri中的单词数量

时间:2018-03-24 11:54:49

标签: php

我必须将结果显示为 दुईईर्बसतासीअर्बबन्ठान्नब्बेकरोडनब्बेलाखखाह्रहजारतीनसयपचहत्तर... 它正确显示结果直到五个数字.. 但是当我们使用六个数字时,它不会根据需要显示.... 比如111111 ....显示“एकसयएघारहजारएकसयएघार” 它必须是“एकलाखएघारहजारएकसयएघार”..... 在七个或更多数字的情况下,它会引发错误.... 错误'未定义的偏移量:1000000 in .....'...

    <?php
   function convert_number_to_words($number) {

   $hyphen      = '-';
   $conjunction = '  ';
   $separator   = ' ';
   $negative    = 'negative ';
   $decimal     = ' point ';
   $dictionary  = array(
    0                   => 'शुन्य',
    1                   => 'एक',
    2                   => 'दुई',
    3                   => 'तीन',
    4                   => 'चार',
    5                   => 'पाँच',
    6                   => 'छ',
    7                   => 'सात',
    8                   => 'आठ',
    9                   => 'नौ',
    10                  => 'दश',
    11                  => 'एघार',
    12                  => 'बाह्र',
    13                  => 'तेह्र',
    14                  => 'चौध',
    15                  => 'पन्ध',
    16                  => 'सोह्र',
    17                  => 'सत्र',
    18                  => 'अठार',
    19                  => 'उन्नाइस',
    20                  => 'विस',
    21                  => 'एक्काइस',
    22                  => 'बाइस',
    23                  => 'तेइस',
    24                  => 'चौविस',
    25                  => 'पच्चिस',
    26                  => 'छब्बिस',
    27                  => 'सत्ताइस',
    28                  => 'अठ्ठाईस',
    29                  => 'उनन्तिस',
    30                  => 'तिस',
    31                  => 'एकत्तिस',
    32                  => 'बत्तिस',
    33                  => 'तेत्तिस',
    34                  => 'चौँतिस ',
    35                  => 'पैँतिस',
    36                  => 'छत्तिस',
    37                  => 'सैँतीस',
    38                  => 'अठतीस',
    39                  => 'उनन्चालीस',
    40                  => 'चालीस',
    41                  => 'एकचालीस',
    42                  => 'बयालीस',
    43                  => 'त्रियालीस',
    44                  => 'चवालीस',
    45                  => 'पैँतालीस',
    46                  => 'छयालीस',
    47                  => 'सरचालीस',
    48                  => 'अठचालीस',
    49                  => 'उनन्चास',
    50                  => 'पचास',
    51                  => 'एकाउन्न',
    52                  => 'बाउन्न',
    53                  => 'त्रिपन्न',
    54                  => 'चउन्न',
    55                  => 'पचपन्न',
    56                  => 'छपन्न',
    57                  => 'सन्ताउन्न ',
    58                  => 'अन्ठाउन्न',
    59                  => 'उनन्साठी',
    60                  => 'साठी',
    61                  => 'एकसट्ठी',
    62                  => 'बयसट्ठी',
    63                  => 'त्रिसट्ठी',
    64                  => 'चौंसट्ठी',
    65                  => 'पैंसट्ठी',
    66                  => 'छयसट्ठी',
    67                  => 'सतसट्ठी',
    68                  => 'अठसट्ठी',
    69                  => 'उनन्सत्तरी',
    70                  => 'सत्तरी',
    71                  => 'एकहत्तर',
    72                  => 'बहत्तर',
    73                  => 'त्रिहत्तर',
    74                  => 'चौहत्तर',
    75                  => 'पचहत्तर',
    76                  => 'छयहत्तर',
    77                  => 'सतहत्तर',
    78                  => 'अठहत्तर',
    79                  => 'उनासी',
    80                  => 'असी',
    81                  => 'एकासी',
    82                  => 'बयासी',
    83                  => 'त्रियासी',
    84                  => 'चौरासी',
    85                  => 'पचासी',
    86                  => 'छयासी',
    87                  => 'सतासी',
    88                  => 'अठासी',
    89                  => 'उनान्नब्बे',
    90                  => 'नब्बे',
    91                  => 'एकान्नब्बे',
    92                  => 'बयानब्बे',
    93                  => 'त्रियान्नब्बे',
    94                  => 'चौरान्नब्बे',
    95                  => 'पन्चानब्बे',
    96                  => 'छयान्नब्बे',
    97                  => 'सन्तान्नब्बे',
    98                  => 'अन्ठान्नब्बे',
    99                  => 'उनान्सय',
    100                 => 'सय',
    1000                => 'हजार',
    100000              => 'लाख',
    10000000            => 'करोड',
    1000000000          => 'अर्ब',
    100000000000        => 'खर्ब'
       );

        if (!is_numeric($number)) {
        return false;
        }

         if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - 
      PHP_INT_MAX) {

        trigger_error(
            'convert_number_to_words only accepts numbers between -' . 
      PHP_INT_MAX . ' and ' . PHP_INT_MAX,
        E_USER_WARNING
        );
        return false;
       }

     if ($number < 0) {
      return $negative . convert_number_to_words(abs($number));
      }

     $string = $fraction = null;

    if (strpos($number, '.') !== false) {
     list($number, $fraction) = explode('.', $number);
    }

     switch (true) {
     case $number < 100:
        $string = $dictionary[$number];
        break;
     case $number < 100:
        $tens   = ((int) ($number / 10)) * 10;
        $units  = $number % 10;
        $string = $dictionary[$tens];
        if ($units) {
            $string .= $hyphen . $dictionary[$units];
        }
        break;
     case $number < 1000:
        $hundreds  = $number / 100;
        $remainder = $number % 100;
        $string = $dictionary[$hundreds] . ' ' . $dictionary[100];
        if ($remainder) {
            $string .= $conjunction . convert_number_to_words($remainder);
        }
        break;;
       default:
        $baseUnit = pow(100000, floor(log($number, 100000)));
        $numBaseUnits = (int) ($number / $baseUnit);
        $remainder = $number % $baseUnit;
        $string = convert_number_to_words($numBaseUnits) . ' ' . 
       $dictionary[$baseUnit];
        if ($remainder) {
            $string .= $remainder < 100 ? $conjunction : $separator;
            $string .= convert_number_to_words($remainder);
        }
        break;
      }

     if (null !== $fraction && is_numeric($fraction)) {
     $string .= $decimal;
     $words = array();
     foreach (str_split((string) $fraction) as $number) {
        $words[] = $dictionary[$number];
       }
       $string .= implode(' ', $words);
      }

      return $string;

      }

      ?>

      Page "numtoword.php"


      Page "index.php"
         <?php
        include('numword.php');
         echo convert_number_to_words(287989012375);

         ?>

1 个答案:

答案 0 :(得分:0)

看起来你让它变得比它需要的更难。我创建了一个更简单的递归函数,将字典和设置移到函数外部(在每个函数调用上生成一个字典数组占用大量内存而不是必需的)。我没有包含分数,但这应该是一个很好的起点:

<?php

$devnagriNumeralWords = [
    'dictionary' => [
        'smallUnits' => [
            0                   => 'शुन्य',
            1                   => 'एक',
            2                   => 'दुई',
            3                   => 'तीन',
            4                   => 'चार',
            5                   => 'पाँच',
            6                   => 'छ',
            7                   => 'सात',
            8                   => 'आठ',
            9                   => 'नौ',
            10                  => 'दश',
            11                  => 'एघार',
            12                  => 'बाह्र',
            13                  => 'तेह्र',
            14                  => 'चौध',
            15                  => 'पन्ध',
            16                  => 'सोह्र',
            17                  => 'सत्र',
            18                  => 'अठार',
            19                  => 'उन्नाइस',
            20                  => 'विस',
            21                  => 'एक्काइस',
            22                  => 'बाइस',
            23                  => 'तेइस',
            24                  => 'चौविस',
            25                  => 'पच्चिस',
            26                  => 'छब्बिस',
            27                  => 'सत्ताइस',
            28                  => 'अठ्ठाईस',
            29                  => 'उनन्तिस',
            30                  => 'तिस',
            31                  => 'एकत्तिस',
            32                  => 'बत्तिस',
            33                  => 'तेत्तिस',
            34                  => 'चौँतिस ',
            35                  => 'पैँतिस',
            36                  => 'छत्तिस',
            37                  => 'सैँतीस',
            38                  => 'अठतीस',
            39                  => 'उनन्चालीस',
            40                  => 'चालीस',
            41                  => 'एकचालीस',
            42                  => 'बयालीस',
            43                  => 'त्रियालीस',
            44                  => 'चवालीस',
            45                  => 'पैँतालीस',
            46                  => 'छयालीस',
            47                  => 'सरचालीस',
            48                  => 'अठचालीस',
            49                  => 'उनन्चास',
            50                  => 'पचास',
            51                  => 'एकाउन्न',
            52                  => 'बाउन्न',
            53                  => 'त्रिपन्न',
            54                  => 'चउन्न',
            55                  => 'पचपन्न',
            56                  => 'छपन्न',
            57                  => 'सन्ताउन्न ',
            58                  => 'अन्ठाउन्न',
            59                  => 'उनन्साठी',
            60                  => 'साठी',
            61                  => 'एकसट्ठी',
            62                  => 'बयसट्ठी',
            63                  => 'त्रिसट्ठी',
            64                  => 'चौंसट्ठी',
            65                  => 'पैंसट्ठी',
            66                  => 'छयसट्ठी',
            67                  => 'सतसट्ठी',
            68                  => 'अठसट्ठी',
            69                  => 'उनन्सत्तरी',
            70                  => 'सत्तरी',
            71                  => 'एकहत्तर',
            72                  => 'बहत्तर',
            73                  => 'त्रिहत्तर',
            74                  => 'चौहत्तर',
            75                  => 'पचहत्तर',
            76                  => 'छयहत्तर',
            77                  => 'सतहत्तर',
            78                  => 'अठहत्तर',
            79                  => 'उनासी',
            80                  => 'असी',
            81                  => 'एकासी',
            82                  => 'बयासी',
            83                  => 'त्रियासी',
            84                  => 'चौरासी',
            85                  => 'पचासी',
            86                  => 'छयासी',
            87                  => 'सतासी',
            88                  => 'अठासी',
            89                  => 'उनान्नब्बे',
            90                  => 'नब्बे',
            91                  => 'एकान्नब्बे',
            92                  => 'बयानब्बे',
            93                  => 'त्रियान्नब्बे',
            94                  => 'चौरान्नब्बे',
            95                  => 'पन्चानब्बे',
            96                  => 'छयान्नब्बे',
            97                  => 'सन्तान्नब्बे',
            98                  => 'अन्ठान्नब्बे',
            99                  => 'उनान्सय'
        ],
        'largeUnits' => [
            100                 => 'सय',
            1000                => 'हजार',
            100000              => 'लाख',
            10000000            => 'करोड',
            1000000000          => 'अर्ब',
            100000000000        => 'खर्ब'
        ]
    ],
    'hyphen'                => '-',
    'conjunction'           => '  ',
    'separator'             => ' ',
    'negative'              => 'negative ',
    'decimal'               => ' point '
];

function convert_devnagri_number_to_words($number) {
    global $devnagriNumeralWords;

    $numLen = strlen($number);
    $output = [ 'prefix' => '', 'base' => '', 'suffix' => '' ];

    $base = pow(10, $numLen-1);

    $smallestLargeUnit = array_keys($devnagriNumeralWords['dictionary']['largeUnits'])[0];

    $isNegative = $number < 0;
    if( $isNegative ) {
        $output['prefix'] .= $devnagriNumeralWords['negative'];
        $number = abs($number);
    }

    if( $number < $smallestLargeUnit ) {
        // Number is less than 100

        $prefix = $number;

    } else {
        // Get base amount (100, 1000, 100000, etc)

        if( isset($devnagriNumeralWords['dictionary']['largeUnits'][$base]) ) {
            $largeUnit = $devnagriNumeralWords['dictionary']['largeUnits'][$base];
        } else {
            // No exact large unit match, find next smallest
            while( $base >= $smallestLargeUnit ) {
                $base = (int) ($base / 10);
                if( isset($devnagriNumeralWords['dictionary']['largeUnits'][$base]) ) {
                    break; // Found a match!
                }
            }
        }

        $output['base'] .= $devnagriNumeralWords['dictionary']['largeUnits'][$base];

        // Get prefix, e.g. "28" when number is 28564
        $prefix = (int) ($number / $base);

        // Subtract prefix to get next part to process
        // E.g. the next number is 345 when the number is 85345
        $nextNumber = $number - ($prefix * $base);
        if( $nextNumber > 0 )
            $output['suffix'] = convert_devnagri_number_to_words($nextNumber);

    }

    if( $prefix >= 0 ) {
        $output['prefix'] .= $devnagriNumeralWords['dictionary']['smallUnits'][$prefix];
    }

    // Put it all together!
    return implode( $devnagriNumeralWords['separator'], array_filter($output) );

}

echo convert_devnagri_number_to_words(287989012375);