PHP Classes
Icontem

File: spelloutnumbers.php


  Search   All class groups All class groups   Latest entries Latest entries   Top 10 charts Top 10 charts   Newsletter Newsletter   Blog Blog   Forums Forums   Help FAQ Help FAQ  
  Login   Register  
Recommend this page to a friend! ReTweet ReTweet Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of DrDoc  >  SpellOutNumbers  >  spelloutnumbers.php  
File: spelloutnumbers.php
Role: Class source
Content type: text/plain
Description: The class itself
Class: SpellOutNumbers
Convert numbers to text, w/ or w/o ordinal suffix.
 

Contents

Class file image Download
<?php
/*
This class may be used for free for non-commercial purposes.
Commercial use requires a one-time fee of $50 per Web site.
Paypal: soaking@spray.se
*/

class spelloutnumbers {

function 
hundreds($number) {
$lasts = array('one','two','three','four','five','six','seven','eight','nine');
$teens = array('eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen');
$tens = array('ten','twenty-','thirty-','forty-','fifty-','sixty-','seventy-','eighty-','ninety-');

$string "";
$j strlen($number);
$done false;
for(
$i=0$i<strlen($number); $i++) {
if(
$j==2) {
if(
strlen($number)>2) {
if(
$number[0]!=0) {$string .= ' hundred ';}
}
if(
$number[$i]==1) {
if(
$number[$i+1]==0) {$string .= $tens[$number[$i]-1];}
else {
$string .= $teens[$number[$i+1]-1];
$done true;
}
}
else {
if(!empty(
$tens[$number[$i]-1])) {$string .= $tens[$number[$i]-1].' ';}
}
}
elseif(
$number[$i]!=&& !$done) {$string .= $lasts[$number[$i]-1];}
$j--;
}
return 
$string;
}    

function 
spellout($number,$suffix=0,$caps=1,$uk=0) {
$many = array(''' thousand ',' million ',' billion ',' trillion ');

$number strval($number);
$string "";

if(
strlen($number)%3!=0) {
$string .= $this->hundreds(substr($number,0,strlen($number)%3));
$string .= $many[floor(strlen($number)/3)];
}

for(
$i=0$i<floor(strlen($number)/3); $i++) {
$string .= $this->hundreds(substr($number,strlen($number)%3+($i*3),3));
if(
$number[strlen($number)%3+($i*3)]!=0) {
$string .= $many[floor(strlen($number)/3)-1-$i];
}
}

$match = array('/ +/','/- /','/-$/','/-thousand/','/-million/','/-billion/','/-trillion/');
$replace = array(' ','-','',' thousand',' million',' billion',' trillion');
$string preg_replace($match,$replace,$string);

if(
$uk) {
$match = array('/billion/','/trillion/');
$replace = array('milliard','billion');
$string preg_replace($match,$replace,$string);
}

if(
$suffix) {
$match = array('/ve$/','/t$/','/y$/','/one$/','/two$/','/three$/','/e$/');
$replace = array('fth','th','ieth','first','second','third','th');
$string preg_replace($match,$replace,$string);
if(!
preg_match("/first$|second$|third$|th$/",$string)) {
$string .= "th";
}
}

if(
$caps) {
$string ucwords($string);
$string preg_replace("/(-[A-Z])/",strtolower("\\1"),$string);
}

$foo strrpos($string," ");
if(
$foo!==false) {
$string substr($string,0,$foo)." and".substr($string,$foo);
}

return 
$string;
}
}
?>

 
  Advertise on this site Advertise on this site   Site map Site map   Statistics Statistics   Site tips Site tips   Privacy policy Privacy policy   Contact Contact  

For more information send a message to :
info at phpclasses dot org.
Copyright (c) Icontem 1999-2009 PHP Classes - PHP Class Scripts
  PHP Book Reviews - Reviews of books and other products