开发者

Is there any php function to convert textual numbers to numbers? For example: convert("nine") outputs 9 [duplicate]

This question already has answers h开发者_JAVA百科ere: Closed 12 years ago.

Possible Duplicate:

Converting words to numbers in PHP

I need a function to convert textual numbers to numbers. For example: convert("nine") outputs 9

I know I could write a function like for example if ($number == "one") { $number = 1; } etc... but that would be a lot of work.


Use the switch statement.


Use a lookup table like this:

$numbers = array(
    'zero' => 0,
    'one' => 1,
    'two' => 2,
    'three' => 3,
    'four' => 4,
    'five' => 5,
    'six' => 6,
    'seven' => 7,
    'eight' => 8,
    'nine' => 9
);

$digit = strtolower($digit);

if (isset($numbers[$digit])) {
    $digit = $numbers[$digit];
}

Note I use strtolower just in case. Of course, this solution would be impractical for anything over a couple dozen. Beyond that, you'll need some sort of parser.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜