Numeric words in a string into numbers

前端 未结 2 827
再見小時候
再見小時候 2020-12-18 10:54

I have string:

$string = \'Five People\';

I want to replace all number-words into numbers. So results are:

$strin         


        
2条回答
  •  天涯浪人
    2020-12-18 11:17

    You can use this regex:

    \b(zero|a|one|tw(elve|enty|o)|th(irt(een|y)|ree)|fi(ft(een|y)|ve)|(four|six|seven|nine)(teen|ty)?|eight(een|y)?|ten|eleven|forty|hundred|thousand|(m|b)illion|and)+\b
    

    By the way, there might be a better regex out there. Until someone posts it, you can use the following implementation

    $regex = '/\b(zero|a|one|tw(elve|enty|o)|th(irt(een|y)|ree)|fi(ft(een|y)|ve)|(four|six|seven|nine)(teen|ty)?|eight(een|y)?|ten|eleven|forty|hundred|thousand|(m|b)illion|and)+\b/i';
    function word_numbers_to_numbers($string) {
        return preg_replace_callback($regex, function($m) {
                   return words_to_number($m[0]);
               },$string);
    }
    

提交回复
热议问题