string handling
|number to words
| see demonstration of number to wordsthis function spells out numbers in english text.
number to words conversion.
full source of number to words [ line 1 - 86 ] | download number to words
| 1 | <?php |
| 2 | $nwords = array( "zero", "one", "two", "three", "four", "five", "six", "seven", |
| 3 | "eight", "nine", "ten", "eleven", "twelve", "thirteen", |
| 4 | "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", |
| 5 | "nineteen", "twenty", 30 => "thirty", 40 => "forty", |
| 6 | 50 => "fifty", 60 => "sixty", 70 => "seventy", 80 => "eighty", |
| 7 | 90 => "ninety" ); |
| 8 | function int_to_words($x) |
| 9 | { |
| 10 | global $nwords; |
| 11 | if(!is_numeric($x)) |
| 12 | { |
| 13 | $w = '#'; |
| 14 | }else if(fmod($x, 1) != 0) |
| 15 | { |
| 16 | $w = '#'; |
| 17 | }else{ |
| 18 | if($x < 0) |
| 19 | { |
| 20 | $w = 'minus '; |
| 21 | $x = -$x; |
| 22 | }else{ |
| 23 | $w = ''; |
| 24 | } |
| 25 | if($x < 21) |
| 26 | { |
| 27 | $w .= $nwords[$x]; |
| 28 | }else if($x < 100) |
| 29 | { |
| 30 | $w .= $nwords[10 * floor($x/10)]; |
| 31 | $r = fmod($x, 10); |
| 32 | if($r > 0) |
| 33 | { |
| 34 | $w .= '-'. $nwords[$r]; |
| 35 | } |
| 36 | } else if($x < 1000) |
| 37 | { |
| 38 | $w .= $nwords[floor($x/100)] .' hundred'; |
| 39 | $r = fmod($x, 100); |
| 40 | if($r > 0) |
| 41 | { |
| 42 | $w .= ' and '. int_to_words($r); |
| 43 | } |
| 44 | } else if($x < 1000000) |
| 45 | { |
| 46 | $w .= int_to_words(floor($x/1000)) .' thousand'; |
| 47 | $r = fmod($x, 1000); |
| 48 | if($r > 0) |
| 49 | { |
| 50 | $w .= ' '; |
| 51 | if($r < 100) |
| 52 | { |
| 53 | $w .= 'and '; |
| 54 | } |
| 55 | $w .= int_to_words($r); |
| 56 | } |
| 57 | } else { |
| 58 | $w .= int_to_words(floor($x/1000000)) .' million'; |
| 59 | $r = fmod($x, 1000000); |
| 60 | if($r > 0) |
| 61 | { |
| 62 | $w .= ' '; |
| 63 | if($r < 100) |
| 64 | { |
| 65 | $word .= 'and '; |
| 66 | } |
| 67 | $w .= int_to_words($r); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | return $w; |
| 72 | } |
| 73 | // demonstration |
| 74 | if(isset($_POST['num'])) |
| 75 | { |
| 76 | echo ' |
| 77 | the number reads '.int_to_words($_POST['num']).'<p> |
| 78 | <a href="'.$_SERVER['PHP_SELF'].'">try again</a>'; |
| 79 | }else{ |
| 80 | echo ' |
| 81 | <form method="post" action="'.$_SERVER['PHP_SELF'].'"> |
| 82 | <input type="text" name="num"> |
| 83 | <input type="submit" value="spell number"> |
| 84 | </form>'; |
| 85 | } |
| 86 | ?> |
4 hits by 4 users in the last 30 minutes.