Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well .. i have to admit.. this was interesting to me on a personal level !!! anyway ... here is the code... you can test it here : <a href="http://www.eyerollweb.com/str2digits/" rel="nofollow">http://www.eyerollweb.com/str2digits/</a></p> <p>and here is the code itself:</p> <pre><code>&lt;?php //The Test string $str = "two hundred thousand six hundred and two"; $numbers = array( 'zero' =&gt; 0, 'one' =&gt; 1, 'two' =&gt; 2, 'three' =&gt; 3, 'four' =&gt; 4, 'five' =&gt; 5, 'six' =&gt; 6, 'seven' =&gt; 7, 'eight' =&gt; 8, 'nine' =&gt; 9, 'ten' =&gt; 10, 'eleven' =&gt; 11, 'twelve' =&gt; 12, 'thirteen' =&gt; 13, 'fourteen' =&gt; 14, 'fifteen' =&gt; 15, 'sixteen' =&gt; 16, 'seventeen' =&gt; 17, 'eighteen' =&gt; 18, 'nineteen' =&gt; 19, 'twenty' =&gt; 20, 'thirty' =&gt; 30, 'forty' =&gt; 40, 'fourty' =&gt; 40, // common misspelling 'fifty' =&gt; 50, 'sixty' =&gt; 60, 'seventy' =&gt; 70, 'eighty' =&gt; 80, 'ninety' =&gt; 90, 'hundred' =&gt; 100, 'thousand' =&gt; 1000, 'million' =&gt; 1000000, 'billion' =&gt; 1000000000); //first we remove all unwanted characters... and keep the text $str = preg_replace("/[^a-zA-Z]+/", " ", $str); //now we explode them word by word... and loop through them $words = explode(" ", $str); //i devide each thousands in groups then add them at the end //For example 2,640,234 "two million six hundred and fourty thousand two hundred and thirty four" //is defined into 2,000,000 + 640,000 + 234 //the $total will be the variable were we will add up to $total = 1; //flag to force the next operation to be an addition $force_addition = false; //hold the last digit we added/multiplied $last_digit = null; //the final_sum will be the array that will hold every portion "2000000,640000,234" which we will sum at the end to get the result $final_sum = array(); foreach ($words as $word) { //if its not an and or a valid digit we skip this turn if (!isset($numbers[$word]) &amp;&amp; $word != "and") { continue; } //all small letter to ease the comparaison $word = strtolower($word); //if it's an and .. and this is the first digit in the group we set the total = 0 //and force the next operation to be an addition if ($word == "and") { if ($last_digit === null) { $total = 0; } $force_addition = true; } else { //if its a digit and the force addition flag is on we sum if ($force_addition) { $total += $numbers[$word]; $force_addition = false; } else { //if the last digit is bigger than the current digit we sum else we multiply //example twenty one =&gt; 20+1, twenty hundred 20 * 100 if ($last_digit !== null &amp;&amp; $last_digit &gt; $numbers[$word]) { $total += $numbers[$word]; } else { $total *= $numbers[$word]; } } $last_digit = $numbers[$word]; //finally we distinguish a group by the word thousand, million, billion &gt;= 1000 ! //we add the current total to the $final_sum array clear it and clear all other flags... if ($numbers[$word] &gt;= 1000) { $final_sum[] = $total; $last_digit = null; $force_addition = false; $total = 1; } } } // there is your final answer ! $final_sum[] = $total; print "Final Answer: " . array_sum($final_sum) . "\n"; ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload