转换字符串“十”至整数 10

发布于 2024-10-03 08:03:09 字数 265 浏览 6 评论 0原文

可能的重复:
在 PHP 中将单词转换为数字

有没有一个 PHP 函数可以转换字符串将“十”转换为整数?
如果没有那么你会怎么做?
尝试过 google 和 php.net 搜索但没有成功

Possible Duplicate:
Converting words to numbers in PHP

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无名指的心愿 2024-10-10 08:03:09

这是我刚刚为此编写的概念证明类...检查一下:

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 扩展来支持它们...如果您愿意,我可以快速修改它以处理与您想要的一样高的数字。

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

}

And a test:

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

And the results:

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

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.

桜花祭 2024-10-10 08:03:09
function smallTextIntToDigits($x)
{
   $lilx = lcase($x)
   $data = array('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);
   if (isset($data[$lilx])) 
       return $data[$lilx];
   elseif (isset(data[lcase(str_replace('teen', '', $x))]) 
       return data[(str_replace('teen', '', lcase($x))] + 10;

   return -1; //harder stuff, recurse on big ones.  blah blah blah
}
function smallTextIntToDigits($x)
{
   $lilx = lcase($x)
   $data = array('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);
   if (isset($data[$lilx])) 
       return $data[$lilx];
   elseif (isset(data[lcase(str_replace('teen', '', $x))]) 
       return data[(str_replace('teen', '', lcase($x))] + 10;

   return -1; //harder stuff, recurse on big ones.  blah blah blah
}
在风中等你 2024-10-10 08:03:09

您可以使用关联数组(也称为哈希)来包含您期望的值。例如:

$assoc_arr = array("one" => 1, "ten" => 10);
$my_ans = $assoc_arr["ten"] + 5;

You can use an associative array (aka hash) to contain the values you expect. For example:

$assoc_arr = array("one" => 1, "ten" => 10);
$my_ans = $assoc_arr["ten"] + 5;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文