Jump to content

counting words


sonnieboy

Recommended Posts

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";

?>

Link to comment
https://forums.phpfreaks.com/topic/195928-counting-words/
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/195928-counting-words/#findComment-1029174
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/195928-counting-words/#findComment-1029183
Share on other sites

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,

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/195928-counting-words/#findComment-1029245
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/195928-counting-words/#findComment-1029252
Share on other sites

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/195928-counting-words/#findComment-1029256
Share on other sites

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.

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/195928-counting-words/#findComment-1029268
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/195928-counting-words/#findComment-1029286
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.