如何使用我的函数将单词转换成数字?

时间:2021-01-13 18:01:34

I created this function to converting numbers to words. And how I can convert words to number using this my function:

我创建了这个函数来将数字转换成文字。我如何用这个函数把单词转换成数字

Simple function code:

简单的函数代码:

$array = array("1"=>"ЯК","2"=>"ДУ","3"=>"СЕ","4"=>"ЧОР","5"=>"ПАНҶ","6"=>"ШАШ","7"=>"ҲАФТ","8"=>"ХАШТ","9"=>"НӮҲ","0"=>"НОЛ","10"=>"ДАҲ","20"=>"БИСТ","30"=>"СИ","40"=>"ЧИЛ","50"=>"ПАНҶОҲ","60"=>"ШАСТ","70"=>"ҲАФТОД","80"=>"ХАШТОД","90"=>"НАВАД","100"=>"САД");
  $n = "98"; // Input number to converting

if($n < 10 && $n > -1){ 
    echo $array[$n];
}
if($n == 10 OR $n == 20 OR $n == 30 OR $n == 40 OR $n == 50 OR $n == 60 OR $n == 70 OR $n == 80 OR $n == 90 OR $n == 100){
    echo $array[$n];
}

if(mb_strlen($n) == 2 && $n[1] != 0) 
{
    $d = $n[0]."0";
    echo "$array[$d]У ".$array[$n[1]];
}

My function so far converts the number to one hundred. How can I now convert text to a number using the answer of my function?

到目前为止,我的函数将数字转换为100。如何使用函数的答案将文本转换为数字?

2 个解决方案

#1


2  

So, as @WillParky93 assumed, your input has spaces between words.

因此,正如@WillParky93假设的那样,您的输入在单词之间有空格。

<?php
mb_internal_encoding("UTF-8");//For testing purposes
$array = array("1"=>"ЯК","2"=>"ДУ","3"=>"СЕ","4"=>"ЧОР","5"=>"ПАНҶ","6"=>"ШАШ","7"=>"ҲАФТ","8"=>"ХАШТ","9"=>"НӮҲ","0"=>"НОЛ","10"=>"ДАҲ","20"=>"БИСТ","30"=>"СИ","40"=>"ЧИЛ","50"=>"ПАНҶОҲ","60"=>"ШАСТ","70"=>"ҲАФТОД","80"=>"ХАШТОД","90"=>"НАВАД","100"=>"САД");
$postfixes = array("3" => "ВУ");
$n = "98"; // Input number to converting
$res = "";
//I also optimized your conversion of numbers to words
if($n > 0 && ($n < 10 || $n%10 == 0))
{
    $res = $array[$n];
}  


if($n > 10 && $n < 100 && $n%10 != 0) 
{
    $d = intval(($n/10));
    $sd = $n%10;

    $ending = isset($postfixes[$d]) ? $postfixes[$d] : "У";
    $res = ($array[$d * 10]).$ending." ".$array[$sd];
}

echo $res;

echo "\n<br/>";
$splitted = explode(" ", $res);

//According to your example, you use only numerals that less than 100
//So, to simplify your task(btw, according to Google, the language is tajik
//and I don't know the rules of building numerals in this language)
if(sizeof($splitted) == 1) {
    echo array_search($splitted[0], $array);
}
else if(sizeof($splitted) == 2) {
    $first = $splitted[0];
    $first_length = mb_strlen($first);
    if(mb_substr($first, $first_length - 2) == "ВУ")
    {
        $first = mb_substr($first, 0, $first_length - 2);
    }
    else
    {
        $first = mb_substr($splitted[0], 0, $first_length - 1);
    }

    $second = $splitted[1];
    echo (array_search($first, $array) + array_search($second, $array));
}

#2


2  

You didn't specify the input specs but I took the assumption you want it with a space between the words.

您没有指定输入规格,但我假设您希望在字之间有空格。

   //get our input=>"522"
    $input = "ПАНҶ САД БИСТ ДУ";
    //split it up
    $split = explode(" ", $input);
    //start out output
    $c = 0;
    //set history
    $history = "";
    //loop the words
    foreach($split as &$s){
       $res = search($s);

       //If number is 9 or less, we are going to check if it's with a number
       //bigger than or equal to 100, if it is. We multiply them together
        //else, we just add them.
        if((($res = search($s)) <=9) ){
           //get the next number in the array
           $next = next($split);
           //if the number is >100. set $nextres
             if( ($nextres = search($next)) >= 100){
                //I.E. $c = 5 * 100 = 500
                $c = $nextres * $res;
                //set the history so we skip over it next run
                $history = $next;
             }else{
                 //Single digit on its own
                 $c += $res;
             }
        }elseif($s != $history){
            $c += $res;
        } 
    }
    //output the result
    echo $c;



    function search($s){
        global $array;

        if(!$res = array_search($s, $array)){
           //grab the string length
           $max = strlen($s);
           //remove one character at a time until we find a match
            for($i=0;$i<$max; $i++ ){
                 if($res = array_search(mb_substr($s, 0, -$i),$array)){
                    //stop the loop
                    $i = $max;
                   }
            }       
        } 

        return $res;
    }     

Output is 522.

输出是522。

#1


2  

So, as @WillParky93 assumed, your input has spaces between words.

因此,正如@WillParky93假设的那样,您的输入在单词之间有空格。

<?php
mb_internal_encoding("UTF-8");//For testing purposes
$array = array("1"=>"ЯК","2"=>"ДУ","3"=>"СЕ","4"=>"ЧОР","5"=>"ПАНҶ","6"=>"ШАШ","7"=>"ҲАФТ","8"=>"ХАШТ","9"=>"НӮҲ","0"=>"НОЛ","10"=>"ДАҲ","20"=>"БИСТ","30"=>"СИ","40"=>"ЧИЛ","50"=>"ПАНҶОҲ","60"=>"ШАСТ","70"=>"ҲАФТОД","80"=>"ХАШТОД","90"=>"НАВАД","100"=>"САД");
$postfixes = array("3" => "ВУ");
$n = "98"; // Input number to converting
$res = "";
//I also optimized your conversion of numbers to words
if($n > 0 && ($n < 10 || $n%10 == 0))
{
    $res = $array[$n];
}  


if($n > 10 && $n < 100 && $n%10 != 0) 
{
    $d = intval(($n/10));
    $sd = $n%10;

    $ending = isset($postfixes[$d]) ? $postfixes[$d] : "У";
    $res = ($array[$d * 10]).$ending." ".$array[$sd];
}

echo $res;

echo "\n<br/>";
$splitted = explode(" ", $res);

//According to your example, you use only numerals that less than 100
//So, to simplify your task(btw, according to Google, the language is tajik
//and I don't know the rules of building numerals in this language)
if(sizeof($splitted) == 1) {
    echo array_search($splitted[0], $array);
}
else if(sizeof($splitted) == 2) {
    $first = $splitted[0];
    $first_length = mb_strlen($first);
    if(mb_substr($first, $first_length - 2) == "ВУ")
    {
        $first = mb_substr($first, 0, $first_length - 2);
    }
    else
    {
        $first = mb_substr($splitted[0], 0, $first_length - 1);
    }

    $second = $splitted[1];
    echo (array_search($first, $array) + array_search($second, $array));
}

#2


2  

You didn't specify the input specs but I took the assumption you want it with a space between the words.

您没有指定输入规格,但我假设您希望在字之间有空格。

   //get our input=>"522"
    $input = "ПАНҶ САД БИСТ ДУ";
    //split it up
    $split = explode(" ", $input);
    //start out output
    $c = 0;
    //set history
    $history = "";
    //loop the words
    foreach($split as &$s){
       $res = search($s);

       //If number is 9 or less, we are going to check if it's with a number
       //bigger than or equal to 100, if it is. We multiply them together
        //else, we just add them.
        if((($res = search($s)) <=9) ){
           //get the next number in the array
           $next = next($split);
           //if the number is >100. set $nextres
             if( ($nextres = search($next)) >= 100){
                //I.E. $c = 5 * 100 = 500
                $c = $nextres * $res;
                //set the history so we skip over it next run
                $history = $next;
             }else{
                 //Single digit on its own
                 $c += $res;
             }
        }elseif($s != $history){
            $c += $res;
        } 
    }
    //output the result
    echo $c;



    function search($s){
        global $array;

        if(!$res = array_search($s, $array)){
           //grab the string length
           $max = strlen($s);
           //remove one character at a time until we find a match
            for($i=0;$i<$max; $i++ ){
                 if($res = array_search(mb_substr($s, 0, -$i),$array)){
                    //stop the loop
                    $i = $max;
                   }
            }       
        } 

        return $res;
    }     

Output is 522.

输出是522。