WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Convert number to words
Categories : PHP, Math., Arrays
ROMEL CUZON
Date : Jun 27th 2007
Grade : 3 of 5 (graded 2 times)
Viewed : 2796
File : No file for this code example.
Images : No Images for this code example.
Search : More code by ROMEL CUZON
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

Convert amount in words
note: Only in tens of thousands
usage:
num_words('102654');

<?php
function num_words($number){
   
$ones=array('','ONE','TWO','THREE','FOUR','FIVE','SIX','SEVEN','EIGHT','NINE');
   
$tens=array('','ELEVEN','TWELVE','THIRTEEN','FOURTEEN','FIFTEEN','SIXTEEN','SEVENTEEN','EIGHTEEN','NINETEEN');
   
$tens2=array('','TEN','TWENTY','THIRTY','FORTY','FIFTY','SIXTY','SEVENTY','EIGHTY','NINETY');
   
$tens3=array('','hundred','thousand','million','billion','trillion');

   
$numlenght=strlen($number);
   
$numarray=str_split($number,1);


    if (
$number<10) {
        return
$ones[intval($number)];
    }

   
//tens
   
if ($numlenght==2&&$number<20 && $numarray[1]<>0) {
        return
$tens[$number-10];
    }


    if (
$numlenght==2&&$number<20 && $numarray[1]==0){
        return
$tens2[$numarray[0]];
    }

    if (
$numlenght==2&&$number>19 && $numarray[1]==0){
        return
$tens2[$numarray[0]];
    }

    if (
$numlenght==2&&$number>19 && $numarray[1]<>0) {
        return
$tens2[$numarray[0]]." ".$ones[$numarray[1]];
    }


   
//hundreds
   
if ($numlenght==3) {
       
$x=$numarray[1].$numarray[2];
        return
$ones[$numarray[0]]." HUNDRED ".tens($x);
    }

   
//THOUSANDS
   
if ($numlenght==4){
       
$y=$numarray[1].$numarray[2].$numarray[3];
       
$z=$numarray[2].$numarray[3];
        if (
intval($y)>99){
            return
$ones[$numarray[0]]." THOUSAND ".hundreds($y);
        }else{
            return
$ones[$numarray[0]]." THOUSAND ". tens($z);
        }
    }


   
//tensthousand

   
if ($numlenght==5){
       
$v=$numarray[0].$numarray[1];
       
$y=$numarray[2].$numarray[3].$numarray[4];
       
$z=$numarray[3].$numarray[4];

        if (
intval($y)>99){
            return
tens($v)." THOUSAND ".hundreds($y);
        }else{
            return
tens($v)." THOUSAND ". tens($z);
        }
    }
}

function
tens($number) {
   
$ones=array('','ONE','TWO','THREE','FOUR','FIVE','SIX','SEVEN','EIGHT','NINE');
   
$tens=array('','ELEVEN','TWELVE','THIRTEEN','FOURTEEN','FIFTEEN','SIXTEEN','SEVENTEEN','EIGHTEEN','NINETEEN');
   
$tens2=array('','TEN','TWENTY','THIRTY','FORTY','FIFTY','SIXTY','SEVENTY','EIGHTY','NINETY');
   
$tens3=array('','hundred','thousand','million','billion','trillion');

   
$numlenght=strlen($number);
   
$numarray=str_split($number,1);

    if (
$number<10){
        return
$ones[intval($number)];
    }
    if (
$numlenght==2&&$number<20 && $numarray[1]<>0){
        return
$tens[$number-10];
    }

    if (
$numlenght==2&&$number<20 && $numarray[1]==0){
        return
$tens2[$numarray[0]];
    }

    if (
$numlenght==2&&$number>19 && $numarray[1]==0){
        return
$tens2[$numarray[0]];
    }

    if (
$numlenght==2&&$number>19 && $numarray[1]<>0){
        return
$tens2[$numarray[0]]." ".$ones[$numarray[1]];
    }
}

function
hundreds($number) {
   
$ones=array('','ONE','TWO','THREE','FOUR','FIVE','SIX','SEVEN','EIGHT','NINE');
   
$tens=array('','ELEVEN','TWELVE','THIRTEEN','FOURTEEN','FIFTEEN','SIXTEEN','SEVENTEEN','EIGHTEEN','NINETEEN');
   
$tens2=array('','TEN','TWENTY','THIRTY','FORTY','FIFTY','SIXTY','SEVENTY','EIGHTY','NINETY');
   
$tens3=array('','hundred','thousand','million','billion','trillion');

   
$numlenght=strlen($number);
   
$numarray=str_split($number,1);

    if (
$number<10){
        return
$ones[intval($number)];
    }

   
//tens

   
if ($numlenght==2&&$number<20 && $numarray[1]<>0){
        return
$tens[$number-10];
    }
    if (
$numlenght==2&&$number<20 && $numarray[1]==0){
        return
$tens2[$numarray[0]];
    }

    if (
$numlenght==2&&$number>19 && $numarray[1]==0){
        return
$tens2[$numarray[0]];
    }

    if (
$numlenght==2&&$number>19 && $numarray[1]<>0){
        return
$tens2[$numarray[0]]." ".$ones[$numarray[1]];
    }

   
//hundreds
   
if ($numlenght==3){
       
$x=$numarray[1].$numarray[2];
        return
$ones[$numarray[0]]." HUNDRED ".tens($x);
    }
}
?>



Weighted Random - Random Scripts usually chose one out of each item, and each item have an equal chance to be chosen. But what if you want an item to be chosed more frequently than other?
Categories : PHP, Math., Arrays
PHP Script to find url links in a page
Categories : PHP, URLs, Regexps, Arrays
abs -- Absolute value
Categories : PHP, PHP Functions, Math.
Looping through two arrays
Categories : PHP, Databases, Arrays
Display list of files within current and subdirectories (recursively) showing each file as an anchored link and each directory as a category header.
Categories : Filesystem, Directories, Arrays, PHP
PHP Random rss feeds - selects 49 random feeds from an unlimited list and displays them on your website. It's Ideal for those moments when you got 5 minutes and dont know which one of your feeds to read.
Categories : PHP, Rich Site Summary (RSS), Arrays
Simple way of scaling any image to fit either given width or height.
Categories : PHP, Graphics, Arrays
PHP Dump in html format the contents of one array variable with a recursive list of the nested array variables inside.
Categories : PHP, Arrays, Variables
A database abstraction layer for the PHP Oracle 8 module (available from PHP 3.0.5). It supports persistent connections, fetching rows into arrays, prepare/execute (variable binding) and has a new and improved error interface.
Categories : Databases, Oracle, PHP, Arrays, Variables
CSS style switcher
Categories : PHP, CSS, HTML and PHP, Arrays, Sessions
array -- Create an array
Categories : PHP, PHP Functions, Arrays
Diffusion-Limited Aggregation visualization
Categories : PHP, Graphics, Algorithms, Math.
Dynamic Loading of XML array data into ComboBox and Display XML data using PHP + DOM + Javascript.
Categories : PHP, Java Script, DOM XML, XML, Arrays
Variable serialization and unserialization. Loading and saving variable structures to and from file.
Categories : Arrays, Filesystem, Variables, Strings, PHP
Library of math functions to expand the functionality of PHP3. Version 1.2.1 fixes a major problem with the gcd function.
Categories : Algorithms, PHP, Math.