A PHP Script To Turn Written Numbers Into Normal Numbers

Monday, February 21, 2011

For some reason, I needed a script that would turn a number written as a word, say “sixteen”, into the number written as a number (16). However, no script available on the internet was capable of doing this for very complex numbers, like turning the words seven billion three hundred twenty two million nine hundred eighty five thousand two hundred sixty eight into 7322985268.

So what does that mean? I get to slog out some code and innovate!

So, in the interests and traditions of throwing out free code to the world, I present the most advanced natural language words to numbers algorithm available on the internet, as far as I know.

function wordstonumbers($str) {
$str = str_replace(“-”, ” “, $str);
$str = explode(” “, $str);
$count = count($str);

$units = array(“zero”, “one”, “two”, “three”, “four”, “five”, “six”, “seven”, “eight”, “nine”, “ten”, “eleven”, “twelve”, “thirteen”, “fourteen”, “fifteen”, “sixteen”, “seventeen”, “eighteen”, “nineteen”);

$tens = array(“”, “”, “twenty”, “thirty”, “forty”, “fifty”, “sixty”, “seventy”, “eighty”, “ninety”);

$scales = array(100 => “hundred”, 1000 => “thousand”, 1000000 => “million”, 1000000000 => “billion”, 1000000000000 => “trillion”);

$j = 0;
$subnum = “”;
for ($i = 0; $i < $count; $i++) {
$search = $str[$i];
if (array_search($search, $units)) { $subnum = $subnum . " " . array_search($search, $units); }
else if (array_search($search, $tens)) { $subnum = $subnum . " " . array_search($search, $tens)*10; }
else if (array_search($search, $scales)) { $subnum = $subnum . " " . array_search($search, $scales); }
}
$subnum = trim($subnum);

$second = explode(" ", $subnum);
$seccount = count($second);
$disp = "";

for($i=0; $i < $seccount; $i++) {
$inta = $second[$i];
if ($seccount > 1) { $intb = $second[$i+1]; }
if ($seccount > 2) { $intc = $second[$i+2]; }
if ($seccount > 3) { $intd = $second[$i+3]; }
if ($seccount > 4) { $inte = $second[$i+4]; }

if ($inta[0] == “0″) { $inta = substr($inta, 1); }

# three hundred fifty six thousand -> 3 100 50 6 1000 -> (3*100+50+6)*1000
if ($inta < $intb && $intc < $intb && $intd < $intb && $intb >= 100 && $intb < $inte && $inte >= 1000) {
$disp .= “($inta*$intb+$intc+$intd)*”;
$i = $i+3;
}

# three hundred fifty thousand -> 3 100 50 1000 -> (3*100+50)*1000
else if ($inta < $intb && $intc < $intb && $intb >= 100 && $intb < $intd && $intd >= 1000) {
$disp .= “($inta*$intb+$intc)*”;
$i = $i+2;
}
else if ($inta < $intb && $intb >= 100) {
$disp .= $inta;
if ($bracket == 1) { $disp .= “)”; $bracket = 0; }
$disp .= “*”;
}
else {
if ($bracket == 0 && $inta < 100) {
$disp .= "(";
$bracket = 1;
}
if ($inta > 0) {
$disp .= $inta;
if ($j < $seccount) { $disp .= "+"; }
}
}
}
$char = count($disp);
$rightbumper = substr_count($disp, ")");
$leftbumper = substr_count($disp, "(");
$offsets = $leftbumper - $rightbumper;
for ($i=0; $i<$offsets; $i++) {
$disp .= ")";
}
if (substr($disp, -2) == "+)") { $disp = substr($disp, 0, -2) . ")"; }
if (substr($disp, -1) == "+") { $disp = substr($disp, 0, -1); }
echo $disp;
eval("\$retur = " . $disp . ";");
return $retur;
}

 

That’s all I have today, but it was a few hours of work.

Be Sociable, Share!

 

Liked this Essay?

4 Comments (RSS)

Read them below and add one yourself.

  1. Bill says:

    and I assume the next post will turn numbers into words???

  2. Jared says:

    And yes, that was supposed to be the word “might,” not “mind.”

  3. Peter says:

    I did enjoy it. My PHP comments clearly don’t contain enough profanity.

Leave a Reply

Comment HTML: You can use HTML in comments. I reccomend <blockquote>Quote</blockquote> for quoting what others have said. <b>Text</b> is for bold, <i>Text</i> is for italic, and <a href="url">text</a> is for making links.