1

Let's say I have strings such as:

$ten = 'ten';
$eleven = 'eleven';
$three = 'three';

etc.

Is there a pre-built function which will convert these into numeric strings based on the English language?

i.e.

return process_to_number($ten); // returns 10 as a numeric value
return process_to_number($eleven); // returns 11 as numeric
return process_to_number($three); // returns... you guessed it, 3!

I guess I could write this manually too (since I only have to process between one and sixteen) but I thought I'd check if there was something like this already.

jeffkee
  • 5,106
  • 12
  • 44
  • 76
  • 4
    Possible duplicate? http://stackoverflow.com/questions/1077600/converting-words-to-numbers-in-php – poncha Jun 18 '12 at 14:59
  • 1
    You can count all the consonants in the word? I have tested it, it works quite wel for the first 3, so I assume it works for the rest too. – Nanne Jun 18 '12 at 14:59
  • Why do you have strings instead of numbers in the first place? Just wondering.. – Sherlock Jun 18 '12 at 15:01
  • @Nanne There are two consonants in 'ten', so that won't work will it. – vascowhite Jun 18 '12 at 15:01
  • @Robinv.G. this probably has to do with processing text input received from humans ;) – poncha Jun 18 '12 at 15:02
  • 1
    There is no built-in function in PHP for this, so you'd better juist build a `switch()` statement. This is (given the constraints of only having to capture 1-16) the easiest solution. However, I really wonder what you're trying to do. Is this about user input? What if they enter '1' instead of 'one'? What about typos ('tow' vs 'two')? Can't you just use a dropdown? Anyhow see [this](http://stackoverflow.com/questions/1077600/converting-words-to-numbers-in-php) question. – CodeCaster Jun 18 '12 at 15:02
  • ... consonants? @Robinv.G. good question. I could have it the other way around too, it doesn't matter to me actually. I could use a function that converts 10 to "ten" spelled out. – jeffkee Jun 18 '12 at 15:02
  • I'm using two different CSS grid systems in one, and they get deployed to multiple sites. Some of them have the skeleton (class="seven columns alpha") while others have the old 960 (class="grid_7 alpha") and I need to include both classes. I guess with only up to 16 columns I may as well build a manual switch function, you're right. – jeffkee Jun 18 '12 at 15:05

1 Answers1

0

There isn't a pre-built one, but there is this rather nice piece of code that you could try to reverse engineer? It's a very complicated area, and one algorithm won't work for all languages (Japanese is base 10,000 and not 1,000 - like English - AFAIK).

Richard
  • 1,024
  • 1
  • 7
  • 15
  • Nice code example, but he's looking for words to numbers, not numbers to words! – Brian Jun 18 '12 at 15:09
  • Or how about for($i = 0; $i < PHP_INT_MAX; $i++) if(convert_number_to_words($i) === $ten) return true; ;) – Richard Jun 18 '12 at 15:14