将字符串'ten'转换为整数10 [英] Convert String 'ten' to Integer 10

查看:124
本文介绍了将字符串'ten'转换为整数10的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

在PHP中将单词转换为数字

是否有一个php函数将一个字符串如'ten'转换成一个整数?

如果没有那么你会怎么做?

尝试了google和php.net搜索没有成功

Is there a php function to convert a string such as 'ten' into an integer?
If not then how would you go about it?
Have tried google and php.net search with no success

推荐答案

以下是我刚才写的概念证明课程......请查看:

Here's a proof of concept class that I just wrote to do this... Check it out:

class WordToNumber {

    public $modifiers = array(
        'hundred' => 100,
    );

    public $negatives = array(
        'minus' => true,
        'negative' => true,
    );

    public $numbers = array(
        'zero'  => 0,
        'one'   => 1,
        'two'   => 2,
        'three' => 3,
        'four'  => 4,
        'five'  => 5,
        'six'   => 6,
        'seven' => 7,
        'eight' => 8,
        'nine'  => 9,
        'ten'   => 10,
        'eleven' => 11,
        'twelve' => 12,
        'thirteen' => 13,
        'fourteen' => 14,
        'fifteen'  => 15,
        'sixteen'  => 16,
        'seventeen' => 17,
        'eighteen'  => 18,
        'nineteen'  => 19,
        'twenty'    => 20,
        'thirty'    => 30,
        'forty'    => 40,
        'fifty'     => 50,
        'sixty'     => 60,
        'seventy'   => 70,
        'eighty'    => 80,
        'ninety'    => 90,    
    );

    public $powers = array(
        'thousand' => 1000,
        'million'  => 1000000,
        'billion'  => 1000000000,
    );

    public function __construct() {
    }

    public function parse($string) {
        $string = $this->prepare($string);
        $parts = preg_split('#\s+#', $string, -1, PREG_SPLIT_NO_EMPTY);
        $buffer = 0;
        $lastPower = 1;
        $powers = array(
            1 => 0,
        );
        $isNegative = false;
        foreach ($parts as $part) {
            if (isset($this->negatives[$part])) {
                $isNegative = true;
            } elseif (isset($this->numbers[$part])) {
                $buffer += $this->numbers[$part];
            } elseif (isset($this->modifiers[$part])) {
                $buffer *= $this->modifiers[$part];
            } elseif (isset($this->powers[$part])) {
                if ($buffer == 0) {
                    //Modify last power
                    $buffer = $powers[$lastPower];
                    unset($powers[$lastPower]);
                    $power = $lastPower * $this->powers[$part];
                    $powers[$power] = $buffer;
                    $lastPower = $power;
                    $buffer = 0;
                } else {
                    $powers[$this->powers[$part]] = $buffer;
                    $buffer = 0;
                    $lastPower = $this->powers[$part];
                }
            } else {
                throw new LogicException('Unknown Token Found: '.$part);
            }
        }
        if (!empty($buffer)) {
            $powers[1] = $buffer;
        }
        $total = 0;
        foreach ($powers as $power => $sub) {
            $total += $power * $sub;
        }
        if ($isNegative) {
            $total *= -1;
        }
        return $total;
    }

    protected function prepare($string) {
        $string = preg_replace('#(\s+|-|\band\b)#i', ' ', $string);
        $string = mb_convert_case($string, MB_CASE_LOWER);
        return $string;
    }

}

并且测试:

$parser = new WordToNumber();

$strings = array(
    'one hundred and fifty two',
    'fifteen',
    'one thousand million',
    'four hundred thousand five hundred fourty three',
    'fifteen hundred',
    'one thousand twelve hundred',
    'negative two',
    'minus three hundred and fifty seven thousand four hundred and two',
);

foreach ($strings as $str) {
    echo $parser->parse($str).' - '.$str."\n";
}

结果:

152 - one hundred and fifty two
15 - fifteen
1000000000 - one thousand million
400543 - four hundred thousand five hundred fourty three
1500 - fifteen hundred
2200 - one thousand twelve hundred
-2 - negative two
-357402 - minus three hundred and fifty seven thousand four hundred and two

它不支持大数字(因此为什么亿是最大的功率),但它应该通过使用来轻微修改以支持它们 bcmath 扩展......如果您愿意,我可以快速修改它使用你想要的数字。

It doesn't support huge numbers (hence why billion is the largest power), but it should be trivially modified to support them by using the bcmath extension... If you want, I could quickly modify it to work with numbers as high as you want.

这篇关于将字符串'ten'转换为整数10的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆