AI Zone Admin Forum Add your forum
Simple PHP Examples
 
 

Ok, here’s the code for the function to convert “numeric words” (e.g. “one”, “seventeen”, “four thousand and ninety six”) to numbers, and the link for the page to test it:

http://www.geekcavecreations.com/lToN.php

And the code:

function convert_lToN($in){
  
global $addCollumn;
  if (empty(
$in)) return $in;
  
$numberWords = 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,
                          
"hundred"   => 100,
                          
"thousand"  => 1000,
                          
"million"  => 1000000,
                          
"billion"  => 1000000000,
                          
"and"       => 0
                          
);

  
$wordList explode(" "$in);
  
$out "";
  
$myNumber 0;
  
$outArray = array();
  
$lastComma 0;
  foreach (
$wordList as $index => $word{
    $test 
strtolower(rtrim($word,",.?!"));
    if (!
array_key_exists($test$numberWords)) {
      $outArray[] 
$word;
      continue;
    
}
    
if ($test == "billion"{
      $acCount 
count($addCollumn) - 1;
      
$tmpTotal 0;
      for(
$n $lastComma$n <= $acCount;$n++) {
        $tmpAdd 
$addCollumn[$n];
        
$addCollumn[$n] 0;
        
$tmpTotal += $tmpAdd;
      
}
      $addCollumn[] 
0;
      
$lastComma $n 1;
      continue;
    
}
    
if ($test == "million"{
      $acCount 
count($addCollumn) - 1;
      
$tmpTotal 0;
      for(
$n $lastComma$n <= $acCount;$n++) {
        $tmpAdd 
$addCollumn[$n];
        
$addCollumn[$n] 0;
        
$tmpTotal += $tmpAdd;
      
}
      $addCollumn[$acCount] 
$tmpTotal 1000000;
      
$addCollumn[] 0;
      
$lastComma $n 1;
      continue;
    
}
    
if ($test == "thousand"{
      $acCount 
count($addCollumn) - 1;
      
$tmpTotal 0;
      for(
$n $lastComma$n <= $acCount;$n++) {
        $tmpAdd 
$addCollumn[$n];
        
$addCollumn[$n] 0;
        
$tmpTotal += $tmpAdd;
      
}
      $addCollumn[$acCount] 
$tmpTotal 1000;
      
$addCollumn[] 0;
      
$lastComma $n 1;
      continue;
    
}
    
if ($test == "hundred"{
      $acCount 
count($addCollumn);
      
$addCollumn[$acCount 1] *= 100;
      
$addCollumn[] 0;
      continue;
    
}
    $addCollumn[] 
$numberWords[$test];
    
$outArray[] "ph";
  
}
  $total 
0;
  foreach (
$addCollumn as $num{
    $total 
+= $num;
  
}
  $total 
number_format($total);
  
$phFound false;
  foreach (
$outArray as $place{
    
if ($place == "ph"{
      
if ($phFound) continue;
      
$phFound true;
      
$out .= " $total";
      continue;
    
}
    $out 
.= " $place";
  
}
  
return ltrim($out);

Again, I don’t think it will handle multiple instances of number words, nor will it process things like “999 billion”, but the core functionality works as intended.

 

 
  [ # 1 ]

Awesome job Dave !!

I tested it with

nine hundred and eighty six thousand four hundred and nine

and it got it bang on !!

 

 
  [ # 2 ]

Try it with it’s limit of “nine hundred and ninety nine billion nine hundred and ninety nine million nine hundred and ninety nine thousand nine hundred and ninety nine”. smile
(Note the word “and” in there several times. I made sure to provide for that, as well.)

 

 
  [ # 3 ]

As I had feared, sentences like “There are six billion people in the world, and only one of me.” don’t work. I’ll think about how to address this, and post the corrected code once I figure it out.

 

 
  [ # 4 ]

But, why does that not work, when the limit works?

 

 
 
  login or register to react