PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old August 18th, 2002, 02:51 PM
Mustafa Karaoglu Mustafa Karaoglu is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Adana - Turkey
Posts: 39 Mustafa Karaoglu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via ICQ to Mustafa Karaoglu Send a message via AIM to Mustafa Karaoglu Send a message via Yahoo to Mustafa Karaoglu
Can we write numbers?

When a user write numbers into text box, lets say any number from 0 to 99999 or more, can we show it on the page as we read/spell?
if he writes 456 it should show four hundred and fifty six etc.

Reply With Quote
  #2  
Old August 18th, 2002, 09:51 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
RE: Can we write numbers?

Yes you can parse the number into individual digits and use an array to store the text for each. Note that handling the teens (11-19) requires some special handling at every level.

B

Reply With Quote
  #3  
Old August 18th, 2002, 09:57 PM
bakertrg's Avatar
bakertrg bakertrg is offline
Contributing User
Codewalkers Regular (2000 - 2499 posts)
 
Join Date: Apr 2007
Location: Scottsdale AZ, US
Posts: 2,253 bakertrg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 48 m 45 sec
Reputation Power: 4
Send a message via Yahoo to bakertrg
RE: Can we write numbers?

I got to thinking about this and here is the code. It takes a variable $input and spits it out as words. Could definately be refined (it won't handle a number with comma's yet but you could strip comma's), I also think it will choke on a number that has millions but with a zero value in the thousands, but you just need to add a little exception handling.
php Code:
Original - php Code
  1.  
  2. <?php
  3.  
  4. $teen_array = array ('ten ', 'eleven ', 'twelve ', 'thirteen ', 'fourteen ', 'fifteen ', 'sixteen ', 'seventeen ', 'eightteen ', 'nineteen ');
  5. $word_array = array (' ', 'one ', 'two ', 'three ', 'four ', 'five ', 'six ', 'seven ', 'eight ', 'nine ');
  6. $tens_array = array ('', '', 'twenty ', 'thirty ', 'fourty ', 'fifty ', 'sixty ', 'seventy ', 'eighty ', 'ninety ');
  7.    
  8. function get_text ($text) {
  9.     switch ($text) {
  10.         case 0:
  11.             $text"";
  12.             break;
  13.         case 1:
  14.             $text"thousand ";
  15.             break;
  16.         case 2:
  17.             $text"million ";
  18.             break;
  19.         case 3:
  20.             $text"trillion ";
  21.             break;
  22.     }
  23.     return $text;
  24. }
  25.  
  26. $number = $input;
  27. $num_len = strlen($number);
  28.  
  29. for ($x = $num_len; $x >= 1; $x--){
  30.     $char = substr($number, 0, 1);
  31.     $number = substr($number, 1);
  32.     if ($x % 3 == 2) {
  33.         if ($char == 1){
  34.             $char = substr($number, 0, 1);
  35.             $number = substr($number, 1);
  36.             $x--;
  37.             $output .= $teen_array[$char];
  38.         } else {
  39.             $output .= $tens_array[$char];
  40.         }
  41.     } else {
  42.         $output .= $word_array[$char];
  43.     }
  44.     if ((strlen($number) % 3) == 0){
  45.         $output .= get_text (strlen($number) / 3);
  46.     }
  47.     if ((strlen($number) % 3) == 2 && $char > 0){
  48.         $output .= 'hundred ';
  49.     }
  50.  
  51. }
  52. echo "$input = $output<br>";
  53. ?>

B


Reply With Quote
  #4  
Old August 18th, 2002, 10:22 PM
Mustafa Karaoglu Mustafa Karaoglu is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Adana - Turkey
Posts: 39 Mustafa Karaoglu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via ICQ to Mustafa Karaoglu Send a message via AIM to Mustafa Karaoglu Send a message via Yahoo to Mustafa Karaoglu
RE: Can we write numbers?

Thanks bakertrg,
it works! how can we add "and" conjuctions. because while we read we sometimes use "and".

Reply With Quote
  #5  
Old August 18th, 2002, 10:30 PM
bakertrg's Avatar
bakertrg bakertrg is offline
Contributing User
Codewalkers Regular (2000 - 2499 posts)
 
Join Date: Apr 2007
Location: Scottsdale AZ, US
Posts: 2,253 bakertrg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 48 m 45 sec
Reputation Power: 4
Send a message via Yahoo to bakertrg
RE: Can we write numbers?

Where do you want to add them? You can easily add them after the word hundred at the bottom of the script or the words thousand etc. in the switch statement at the top. Be sure to add a space at the end of any text string you add.

B

Reply With Quote
  #6  
Old August 18th, 2002, 10:35 PM
bakertrg's Avatar
bakertrg bakertrg is offline
Contributing User
Codewalkers Regular (2000 - 2499 posts)
 
Join Date: Apr 2007
Location: Scottsdale AZ, US
Posts: 2,253 bakertrg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 48 m 45 sec
Reputation Power: 4
Send a message via Yahoo to bakertrg
RE: Can we write numbers?

one other thing, you can easily make this work for bigger numbers if you add new cases to the switch statement. The code is basically checking how many blocks of three digits you have and apending the case text as appropriate.

B

Reply With Quote
  #7  
Old August 18th, 2002, 10:43 PM
Mustafa Karaoglu Mustafa Karaoglu is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Adana - Turkey
Posts: 39 Mustafa Karaoglu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via ICQ to Mustafa Karaoglu Send a message via AIM to Mustafa Karaoglu Send a message via Yahoo to Mustafa Karaoglu
RE: Can we write numbers?

normally we read as
125 =one hundred and twenty five
or 105 =one hundred and five
or 1001 =one thousand and one
15972=fiteen thousand nine hundred and sevent two

Reply With Quote
  #8  
Old August 18th, 2002, 10:48 PM
Mustafa Karaoglu Mustafa Karaoglu is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Adana - Turkey
Posts: 39 Mustafa Karaoglu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via ICQ to Mustafa Karaoglu Send a message via AIM to Mustafa Karaoglu Send a message via Yahoo to Mustafa Karaoglu
RE: Can we write numbers?

I have added and after hundred. it spits out 125 ok but when I write 100 it says one "hundred and"

Reply With Quote
  #9  
Old August 18th, 2002, 11:38 PM
bakertrg's Avatar
bakertrg bakertrg is offline
Contributing User
Codewalkers Regular (2000 - 2499 posts)
 
Join Date: Apr 2007
Location: Scottsdale AZ, US
Posts: 2,253 bakertrg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 48 m 45 sec
Reputation Power: 4
Send a message via Yahoo to bakertrg
RE: Can we write numbers?

add an if statement that checks to see if following the hundred the next two characters are not 00.

if (substr($number, 0, 2) == '00'){
$output .= 'hundred ';
} else {
$output .= 'hundred and ';
}

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > Can we write numbers?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |