Is there any php function to convert textual numbers to numbers? For example: convert("nine") outputs 9 [duplicate]
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.
精彩评论