PHP Snippets
numtostr
Information
(PHP Version >= 4.1.0)Functions used in this snippet: strrev, strlen, substr, array_reverse, sizeof, array_key_exists, str_replace, implode.
Description
str numtostr( int num )
Transform the number num into the textual representation of the number.
Snippet
<?php
function numtostr($num){
$big = array( '',
'thousand',
'million',
'billion',
'trillion',
'quadrillion',
'quintillion',
'sextillion',
'septillion');
$small = array( '',
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine',
'ten',
'eleven',
'twelve',
'thirteen',
'fourteen',
'fifteen',
'sixteen',
'seventeen',
'eighteen',
'nineteen');
$other = array( '',
'',
'twenty',
'thirty',
'fourty',
'fifty',
'sixty',
'seventy',
'eighty',
'ninety');
$hun = 'hundred';
$end = array();
$num = strrev($num);
$final = array();
for($i=0; $i<strlen($num); $i+=3){
$end[$i] = strrev(substr($num, $i, 3));
}
$end = array_reverse($end);
for($i=0; $i<sizeof($end); $i++){
$len = strlen($end[$i]);
$temp = $end[$i];
if($len == 3){
$final[] = $temp{0} != '0' ? $small[$end[$i]{0}] . ' ' . $hun : $small[$end[$i]{0}];
$end[$i] = substr($end[$i], 1, 2);
}
if($len > 1){
$final[] = array_key_exists($end[$i], $small) ? $small[$end[$i]] : $other[$end[$i]{0}] . ' ' . $small[$end[$i]{1}];
}else{
$final[] = $small[$end[$i]{0}];
}
$final[] = $temp != '000' ? $big[sizeof($end) - $i - 1] : '';
}
return str_replace(array(' ', ' ', ' ', ' ', ' ', ' ', ' '), ' ', implode(' ',$final));
}
?>
Example
numtostr Can be used in the following way:
<?php
echo numtostr(123);
// one hundred twenty three
echo numtostr(99999999);
//ninety nine million nine hundred ninety nine thousand nine hundred ninety nine
?>
2 Responses to numtostr
Another way to do it with using a recursive function:
<?php
function int_to_words($int) {
$single_words = array(null,
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine',
'ten',
'eleven',
'twelve',
'thirteen',
'fourteen',
'fifteen',
'sixteen',
'seventeen',
'eighteen',
'nineteen');
$ten_words = array(null,
null,
'twenty',
'thirty',
'forty',
'fifty',
'sixty',
'seventy',
'eighty',
'ninety');
if ($int < 1 || $int > 999999999) {
return false;
}
$millions = intval($int / 1000000);
$units = $int - ($millions * 1000000);
if ($millions > 0) {
$result = int_to_words($millions) . ' million';
if ($units > 0) {
$result .= ' ' . int_to_words($units);
}
} else {
$thousands = intval($int / 1000);
$units -= $thousands * 1000;
if ($thousands > 0) {
$result = int_to_words($thousands) . ' thousand';
if ($units > 0) {
$result .= ' ' . int_to_words($units);
}
} else {
$hundreds = intval($units / 100);
$units -= $hundreds * 100;
if ($hundreds > 0) {
$result = int_to_words($hundreds) . ' hundred';
if ($units > 0) {
$result .= ' ' . int_to_words($units);
}
} else {
if ($units <= 19) {
$result = $single_words[$units];
} else {
$tens = intval($units / 10);
$units -= $tens * 10;
$result = $ten_words[$tens];
if ($units > 0) {
$result .= ' ' . int_to_words($units);
}
}
}
}
}
return $result;
}
?>
Looks good too.