1

I know I can do that using switch(), but still I wanted to know if there's any PHP function for that.

hakre
  • 193,403
  • 52
  • 435
  • 836
kapeels
  • 1,692
  • 4
  • 30
  • 52
  • 2
    How far do you want to take it? Beyond 10? – Pekka Jun 05 '10 at 16:59
  • 6
    `function convertTo9($s) { return $s == "nine" ? 9 : null; }` :) – Mark Rushakoff Jun 05 '10 at 17:01
  • People, I wanted to know if there's any function for doing so. I need it till 7 only. I knew how to get it working, but you guys have provided some interesting examples. Thanks to everybody. – kapeels Jun 05 '10 at 17:04
  • 3
    *related* : [Converting words to numbers in PHP](http://stackoverflow.com/questions/1077600/converting-words-to-numbers-in-php) – Felix Kling Jun 05 '10 at 17:04
  • 1
    I'm pretty sure there is some devious way of abusing `strtotime()` for this... But let's better not try to find out. :) – Pekka Jun 05 '10 at 17:05

1 Answers1

9

From one to ten:

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

echo $numbers["nine"]; // 9 

Beyond that, it gets trickier (except if you want to type out a very large list, of course.)

Pekka
  • 442,112
  • 142
  • 972
  • 1,088