0

Hi I was wondering how can I print out numbers in their word form ? When I did a Google search it showed other peoples script on how to print 234 as two hundred thirty four .

I need 234 as two three four.

Thank you guys!

Kory Le
  • 11
  • check this http://www.karlrixon.co.uk/writing/convert-numbers-to-words-with-php/ – Vidya L Oct 14 '14 at 03:51
  • an array, split the string, seems simple, try SOMETHING –  Oct 14 '14 at 03:51
  • I havent tried anything. Im completely new to php. Im getting the numbers passed in from an html form. So far i did $numbers = $_POST['thedata'] – Kory Le Oct 14 '14 at 03:54
  • For all those who marked this as a duplicate, did you actually read the question? **I need 234 as two three four**, and the guy already knows about the scripts available to convert 234 into two hundred and thirty four. – Burhan Khalid Oct 14 '14 at 04:22

1 Answers1

2

I need 234 as two three four.

It would be as simple as creating an array of your number to word map, then taking your number and printing out the corresponding value:

<?php

   $num_word = array();
   $num_word[0] = 'zero';
   $num_word[1] = 'one';
   $num_word[2] = 'two'
   ...
   $num_word[9] = 'nine';

   $num = 234;

   foreach(str_split($num) as $w) {
       echo $num_word[$w];
   }
?>
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • since that the maximum number word that he need is nine, you can do this – Kelvin Barsana Oct 14 '14 at 03:56
  • +1, but don't forget a space when echoing, or you'll just get a strand of words. – Dave Chen Oct 14 '14 at 04:05
  • Thank you Burhan! This is exactly what I was trying to do. Amazing how useful this site can be at an amazing speed too. – Kory Le Oct 14 '14 at 04:25
  • To add onto this, if I wanted to only print certain indexs, would that be possible with a foreach loop? For example if I wanted to print 2345 as three four (only the 2 middle numbers). Sorry my book that I'm currently using isn't very helpful. – Kory Le Oct 14 '14 at 04:34
  • Yes `foreach(str_split($num) as $index => $letter) {` – Burhan Khalid Oct 14 '14 at 04:59