<?php
/*
* Fuzzy Number function
* Written April 29th.
* Takes in a single variable - $num, and processes it, returning plain-text numbers (for instance, give it 32987 and it will return 'thirty two thousand, nine hundred and eighty seven'.
* Licensed under the Creative Commons Attribution-Noncommercial Share-Alike 2.5 license for UK: Scotland
* http://creativecommons.org/licenses/by-nc-sa/2.5/scotland/
* As much as you really don't have to, I'd appreciate a head's-up if you ever decide to use my code anywhere. It'd make me feel all great and such :D

* This script essentially takes a number and processes it into human-language text. I called it fuzzy numbers for...not alot of reasoning.
* This was coded mostly for my local homepage, as I wanted something to display a more human version of my last.fm playcount (now at around 30000).
*/
function fuzzyNum($num) {
    
//FuzzyNumbers function
    //Don't attempt to translate negative numbers
    
if($num 0) {
        return 
"unknown number";
    }
    
//Data arrays for humanized numbers.
    //All these arrays for ease. I'm sure this could be done simpler and without so much conditional code.
    //Singular numbers
    
$numSingles = array(
        
'0' => '',
        
'1' => 'one',
        
'2' => 'two',
        
'3' => 'three',
        
'4' => 'four',
        
'5' => 'five',
        
'6' => 'six',
        
'7' => 'seven',
        
'8' => 'eight',
        
'9' => 'nine',
        
'10' => 'ten'
    
);
    
//Numbers ranging between 11 and 19
    
$numTeens = array (
        
'11' => 'eleven',
        
'12' => 'twelve',
        
'13' => 'thirteen',
        
'14' => 'fourteen',
        
'15' => 'fifteen',
        
'16' => 'sixteen',
        
'17' => 'seventeen',
        
'18' => 'eighteen',
        
'19' => 'nineteen'
    
);
    
//Tens
    
$numDoubles = array(
        
'20' => 'twenty',
        
'30' => 'thirty',
        
'40' => 'forty',
        
'50' => 'fifty',
        
'60' => 'sixty',
        
'70' => 'seventy',
        
'80' => 'eighty',
        
'90' => 'ninety'
    
);
    
//Units of numbers.
    
$numUnits = array(
        
'1' => 'one',
        
'10' => 'ten',
        
'100' => 'hundred',
        
'1000' => 'thousand'
    
);
    
//The easiest part. Ever.
    
if($num <= 10) {
        return 
$numSingles[$num];
    }
    
//Humanize the number if it's under 100.
    
if($num 100) {
        
//Tens
        
$num1 floor($num/10);
        
//Units
        
$num2 $num - ($num1 10);
        
//If it's in the 'teens (11-19).
        
$teens $num1+$num2;
        if(
$teens 10 && $teens 20) {
            return 
$numTeens[$teens];
        }
        return 
$numDoubles[$num1]." ".$numSingles[$num2];
    }
    
//It's over 99. Lazy code ;P
    
if($num == 100) {
        return 
$numSingles['1']." ".$numUnits['100'];
    }
    
//If the number's under 1000.
    
if($num 1000) {
        
//num1 is the hundreds num0 is the hundreds in singular numbers.
        
$num0 floor($num/100);
        
$num1 = (floor($num/100)*100);
        
//num2 is the tens
        
$num2 = (floor(($num $num1)/10)*10);
        
//num3 is the units
        
$num3 = ($num $num1) - $num2;
        
$teens $num2+$num3;
        if(
$teens 10 && $teens 20) {
            return 
$numSingles[$num0]." ".$numUnits['100']." and ".$numTeens[$teens];
        }
        if(
$teens=='0') {
            return 
$numSingles[$num0]." ".$numUnits['100'];
        }
        return 
$numSingles[$num0]." ".$numUnits['100']." and ".$numDoubles[$num2]." ".$numSingles[$num3];
    }
    
//It's in the thousands. Fallback code that only works up to 99,999.
    //Descriptive variable use here :P
    
$thousands floor($num/1000);
    
$num -= $thousands*1000;
    
$thousands1 $thousands - (floor($thousands/10)*10);
    
$hundreds floor($num/100);
    
$num -= $hundreds*100;
    
$tens = (floor($num/10)*10);
    
$num -= $tens;
    
$units $num;
    
$teens $tens+$units;
    
//The largest block of conditional return code ever.
    //You're off your head if you think I'm going to document this part intensely.
    //If it's ?0???
    
if($thousands1=='0') {
        
//if it's ??0??
        
if($hundreds=='0') {
            if(
$teens 10 && $teens 20) {
                return 
$numDoubles[$thousands]." ".$numUnits['1000']." and ".$numTeens[$teens];
            }
            if((
$tens+$units)=='0') {
                return 
$numDoubles[$thousands]." ".$numUnits['1000'];
            }
            return 
$numDoubles[$thousands]." ".$numUnits['1000']." and ".$numDoubles[$tens]." ".$numSingles[$units];
        }
        if(
$teens 10 && $teens 20) {
            return 
$numDoubles[$thousands]." ".$numUnits['1000'].", ".$numSingles[$hundreds]." ".$numUnits['100']." and ".$numTeens[$teens];
        }
        if((
$tens+$units)=='0') {
            return 
$numDoubles[$thousands]." ".$numUnits['1000'].", ".$numSingles[$hundreds]." ".$numUnits['100'];
        }
        return 
$numDoubles[$thousands]." ".$numUnits['1000'].", ".$numSingles[$hundreds]." ".$numUnits['100']." and ".$numDoubles[$tens]." ".$numSingles[$units];
    }
    
$thousands floor($thousands/10)*10;
    if(
$hundreds=='0') {
        if(
$teens 10 && $teens 20) {
            return 
$numDoubles[$thousands]." ".$numSingles[$thousands1]." ".$numUnits['1000']." and ".$numTeens[$teens];
        }
        if((
$tens+$units)=='0') {
            return 
$numDoubles[$thousands]." ".$numSingles[$thousands1]." ".$numUnits['1000'];
        }
        return 
$numDoubles[$thousands]." ".$numSingles[$thousands1]." ".$numUnits['1000']." and ".$numDoubles[$tens]." ".$numSingles[$units];
    }
    if(
$teens 10 && $teens 20) {
        return 
$numDoubles[$thousands]." ".$numSingles[$thousands1]." ".$numUnits['1000'].", ".$numSingles[$hundreds]." ".$numUnits['100']." and ".$numTeens[$teens];
    }
    if((
$tens+$units)=='0') {
        return 
$numDoubles[$thousands]." ".$numSingles[$thousands1]." ".$numUnits['1000'].", ".$numSingles[$hundreds]." ".$numUnits['100'];
    }
    return 
$numDoubles[$thousands]." ".$numSingles[$thousands1]." ".$numUnits['1000'].", ".$numSingles[$hundreds]." ".$numUnits['100']." and ".$numDoubles[$tens]." ".$numSingles[$units];
}
?>