Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    kjc
    kjc is offline
    New Coder
    Join Date
    Jun 2002
    Location
    England
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Montreal, Canada
    Posts
    644
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 08:00 PM.

  • #4
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    WOW! Cool Function!

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    Montreal, Canada
    Posts
    644
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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']);

    ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •