sonnieboy Posted March 20, 2010 Share Posted March 20, 2010 Greetings gurus, This little function that converts numbers to words and counts the number of words is just driving me nuts. The funtion appears to be working well in terms of converting numbers to words except 2 little issues. 1, The array $word lists both the numbers and words from 0 to 10. However, when it displays the words, it omits 10. In other words, instead of dislaying the numbers from 0 to 10 into words zero to ten, it displays them zero to nine. Why is ten being omitted? 2, The second issue is that it is supposed to count the number of words from zero to to (11), it counts them as 21 . What am I doing wrong? Here is the code anad thanks alot in advance. <?php // numbers to words function // example: 7 -> seven // works with 0 to 10 inclusive //Anything below 0 or greater than 10 invites an error $outputStr = ''; function numbertowords($str) { global $outputStr; if(strlen($str) > 1) { for($i=0; $i<strlen($str);$i++) { $outputStr .= numbertowords($str[$i]).' '; } return $outputStr; } $doc = array(0 => "zero", 1 => "one", 2 => "two", 3 => "three", 4 => "four", 5 => "five", 6 => "six", 7 => "seven", 8 => "eight", 9 => "nine", 10 => "ten"); foreach($doc as $key => $val) { if(strtolower($str) == $key || $str == $val) { return $val; } } return "";// returns nothing } $nums = "0123456789"; echo numbertowords($nums),"<br>"; //Now count number of words in a text string $text = numbertowords($nums); $count = count(explode(" ", $text)); echo "Converted text contains $count words"; ?> Quote Link to comment Share on other sites More sharing options...
jskywalker Posted March 20, 2010 Share Posted March 20, 2010 1, The array $word lists both the numbers and words from 0 to 10. However, when it displays the words, it omits 10. $nums = "0123456789"; echo numbertowords($nums),"<br>"; how many numers are there in $nums....... Quote Link to comment Share on other sites More sharing options...
jskywalker Posted March 20, 2010 Share Posted March 20, 2010 2, The second issue is that it is supposed to count the number of words from zero to to (11), it counts them as 21 . $text = numbertowords($nums); $count = count(explode(" ", $text)); echo "Converted text contains $count words"; try changing you code to this: $text = numbertowords($nums); echo "TEST: $text <br>"; $count = count(explode(" ", $text)); echo "Converted text contains $count words"; than you'll know why it says 21 Quote Link to comment Share on other sites More sharing options...
teamatomic Posted March 20, 2010 Share Posted March 20, 2010 Looking at your function outputStr is the problem. At the start of your script you declare outputStr. In your function you concatenate outputStr. You run through your function twice, but inbetween calls you dont reset outputStr, so it concatenates again. Thus the count of 21. Simple. HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
sonnieboy Posted March 20, 2010 Author Share Posted March 20, 2010 Ok, First, sorry jskywalker. I put something different from what I had in mind. I was going to say that I couldn't figure out a way to display 10. For instance, 012345678910 will be translated as zero, one, two...one zero. In other words, the 10 will be translated as one zero instead of ten. Is it possible to manipulate it? As for the values counting twice, please show me how, and where to reset $outputStr. If I do this: $text = $outputStr; $count = count(explode(" ", $text)); echo "Converted text contains $count words"; I get a count of 11, not accurate but close enough but then I don't think that is correct. Sorry guys, still learning. Thanks again, Quote Link to comment Share on other sites More sharing options...
teamatomic Posted March 20, 2010 Share Posted March 20, 2010 You have no need to call it twice, do this: $nums = "0123456789"; $text= numbertowords($nums),"<br>"; echo "$text<p>"; //Now count number of words in a text string $text=trim(text); $count = count(explode(" ", $text)); echo "Converted text contains $count words"; HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
jskywalker Posted March 20, 2010 Share Posted March 20, 2010 you will get 11 because of the last space in $text (which is also counted as a seperator) 01234567 is not "zero one two three ...." but it is 1 milion two hondered thirty four thousand five hundred....." only if you write "0 1 2 3 ...7 8 9 10" than you can get an output of "ten" at the end Quote Link to comment Share on other sites More sharing options...
sonnieboy Posted March 20, 2010 Author Share Posted March 20, 2010 Thank you good people again. I really appreciate it. First Teamatomic, The below code is not giving me the right count. It simply says, Converted text contains 1 words //Now count number of words in a text string $text=trim(text); $count = count(explode(" ", $text)); echo "Converted text contains $count words"; jskywalker, This, "0 1 2 3 ...7 8 9 10", does not give an output of ten at end. It still treats 10 as one zero. Agains, thanks to you two for your help. Quote Link to comment Share on other sites More sharing options...
teamatomic Posted March 20, 2010 Share Posted March 20, 2010 My mistake. $text=trim(text) just turn text to $test in the trim() function line. $text=trim($text) HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
sonnieboy Posted March 20, 2010 Author Share Posted March 20, 2010 Doofus me! I should have seen it myself. I am sorry about that. Thanks a lot for your help. Where does one mark a problem as solved? Again, thanks alot Quote Link to comment Share on other sites More sharing options...
-Karl- Posted March 20, 2010 Share Posted March 20, 2010 At the bottom there will be a button to mark it as solved. Quote Link to comment Share on other sites More sharing options...
teamatomic Posted March 20, 2010 Share Posted March 20, 2010 OK. a bit more info here. I'm not sure where, but if you RTM on globals and functions somewhere in there you will be told not to import with globals into a function. It defeats on of the main purposes of a function, to keep everthing within its own(the functions) scope. //$outputStr = ''; function numbertowords($str) { // global $outputStr; You function will still work and you can call it multiple times in a row because outputStr is now confined to the scope of the function and goes "poof" as soon as the function returns and exits. HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
sonnieboy Posted March 20, 2010 Author Share Posted March 20, 2010 Ok, I did that thinking I was initializing it to zero to start off with. But thanks so much for that info. Quote Link to comment Share on other sites More sharing options...
jskywalker Posted March 21, 2010 Share Posted March 21, 2010 This, "0 1 2 3 ...7 8 9 10", does not give an output of ten at end. It still treats 10 as one zero. Of course you need to change your function. Currently it looks only at 1 position in the input, and the number "10" contains two positions.... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.