Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-12-2003, 03:00 PM   PM User | #1
kjc
New Coder

 
Join Date: Jun 2002
Location: England
Posts: 56
Thanks: 0
Thanked 0 Times in 0 Posts
kjc is an unknown quantity at this point
Getting a string from a number format (eg changing 2 into two)

Does anyone know if it is possible to do this? Ie to change 3 into three and 11 into eleven etc...
__________________
Eternity
kjc is offline   Reply With Quote
Old 04-12-2003, 07:37 PM   PM User | #2
missing-score
Senior Coder


 
missing-score's Avatar
 
Join Date: Jan 2003
Location: UK
Posts: 2,194
Thanks: 0
Thanked 0 Times in 0 Posts
missing-score is on a distinguished road
As far as I know, there is no function for this.

A long way round, but you could use an array of names...

PHP Code:

$nums 
= array('one','two','three','four','five','six','seven','eight','nine');

$num 3;

$num str_replace($num,$nums[($num-1)],$num); 
That should work. depends on how many numbers you wanna compensate for
missing-score is offline   Reply With Quote
Old 04-12-2003, 08:47 PM   PM User | #3
x_goose_x
Regular Coder

 
Join Date: Jun 2002
Location: Montreal, Canada
Posts: 644
Thanks: 0
Thanked 0 Times in 0 Posts
x_goose_x is an unknown quantity at this point
PHP Code:
A correction has been made
Note: the $break3 array can be extended as high as you would like.

Last edited by x_goose_x; 04-13-2003 at 07:00 PM..
x_goose_x is offline   Reply With Quote
Old 04-12-2003, 10:55 PM   PM User | #4
missing-score
Senior Coder


 
missing-score's Avatar
 
Join Date: Jan 2003
Location: UK
Posts: 2,194
Thanks: 0
Thanked 0 Times in 0 Posts
missing-score is on a distinguished road
WOW! Cool Function!
missing-score is offline   Reply With Quote
Old 04-13-2003, 03:34 PM   PM User | #5
x_goose_x
Regular Coder

 
Join Date: Jun 2002
Location: Montreal, Canada
Posts: 644
Thanks: 0
Thanked 0 Times in 0 Posts
x_goose_x is an unknown quantity at this point
Fixed a bug. It wasmessing up when converting the tens and ones, when below 20.

PHP Code:
<?php

function num2words ($num) {
  
$num ereg_replace("[^0-9]"""$num); //removes all non-numeric characters.
  
$break3 = Array ("","Thousand","Million","Billion","Trillion","Quadrillion","Quintillion");
  
$one2twenty = Array ("","One""Two""Three""Four""Five""Six""Seven""Eight""Nine""Ten""Eleven""Twelve""Thirteen""Fourteen""Fifteen""Sixteen""Seventeen""Eighteen""Nineteen");
  
$tens = Array("","","Twenty""Thirty""Fourty""Fifty""Sixty""Seventy""Eighty""Ninety");
  
$num_split = Array();
  while ( 
strlen($num) > ) {
    if ( 
strlen($num) > ) {
      
$num_split[] = substr($num, (strlen($num)-3));
      
$num substr($num0, (strlen($num)-3));
    } else {
      
$num_split[] = $num;
      
$num "";
    }
  }
  
$num_split array_reverse ($num_split);
  
$ret "";
  foreach ( 
$num_split as $key=>$val ) {
    
$suff sizeof($num_split)-$key-1;
    if ( 
$val 20) {
      
$val $one2twenty[$val];
    } else {
      if ( 
$val >= 100 ) {
        
$hun $one2twenty[$val{0}]." Hundred";
        
$ten $val{1};
        
$one $val{2};
      } else {
        
$ten $val{0};
        
$one $val{1};
      }
      if ( 
$ten >= ) {
        
$val $tens[$ten]."-".$one2twenty[$one];
      } else {
        
$val $one2twenty[$ten.$one];
      }
    }
    
$ret .= "$hun $val $break3[$suff]";
    if ( 
$suff != "" $ret .= ", ";
  }
  return 
$ret;
}

print 
num2words($_GET['num']);

?>
x_goose_x is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:46 PM.


Advertisement
Log in to turn off these ads.