ID:               16681
 Updated by:       [EMAIL PROTECTED]
-Summary:          Request new words() function returning no of words in
                   string
 Reported By:      [EMAIL PROTECTED]
 Status:           Open
 Bug Type:         Feature/Change Request
 Operating System: Windows XP Home Edition
 PHP Version:      4.1.2
 New Comment:

I wish to expand this request to suggest full support for word
processing in strings.  This is something which is extremely useful (it
appears for example in REXX) when processing strings.

word (str, num) 
Returns the num'th word from string str. If num is greater than the
number of words in str, the null string is returned. 
 
wordindex (str, num) 
Returns the character position of the first letter of the num'th word
in string str, or 0 if num is greater than the number of characters in
str. 
 
wordlength (str, num) 
Returns the length of the num'th word in string str, or 0 if num is
greater than the number of words in str. 
 
words (str, num)
Returns the number of words in the string str.


Previous Comments:
------------------------------------------------------------------------

[2002-04-19 03:56:33] [EMAIL PROTECTED]

In my opinion this function doesn't that much value to PHP.

------------------------------------------------------------------------

[2002-04-19 02:09:53] [EMAIL PROTECTED]

I would strongly favour the name words() rather than word_count(). 
Though there might be arguments for consistency with other functions to
call it word_count(), I think that words() is the more natural name and
more easily rememebered.

if (words($mystring) > 1) do_something();

You talk abouts "words" in a string and not "word count" in a string.

Hugh

------------------------------------------------------------------------

[2002-04-18 12:08:04] [EMAIL PROTECTED]

I think in PHP it would look like this:

function word_count($string) {
  return count(preg_split("/\s+/", $string));
}

------------------------------------------------------------------------

[2002-04-18 10:15:04] [EMAIL PROTECTED]

I notice that PHP does not have a words() function; I propose that one
is added.

For anybody doing string processing this is an extremely useful
function.  It is a commonly found function in other languages (e.g.
REXX).  It is not just for general text processing that such a function
is useful, but whenever dealing with strings.

For example, I had a string where I had to take different actions if
there was only 1 word in a string than if there were many.  

I agree that you can do this sort of thing with regular expressions,
but hey, you can do a Leveshtein function too with regular expressions
;).

I can guarantee if there was one, it would be widely used.

words(), simply should return the number of words in a string, e.g.

words("hello") -> 1

words("this is my example") -> 4

words("     ah how about this, eh   smartass?") -> 6

words("thank you PHP team for taking the time to read this and giving
due consideration for this suggestion rather than just throwing it in
the waste bin because you've got more urgent things to do") -> 35

Hugh Prior

------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=16681&edit=1

Reply via email to