PHP Classes

File: spelloutnumbers.php

Recommend this page to a friend!
  Classes of DrDoc   SpellOutNumbers   spelloutnumbers.php   Download  
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.
Author: By
Last change: Minor fix
Date: 20 years ago
Size: 2,492 bytes
 

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]!=0 && !$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;
}
}
?>