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:
When I was coding the sample code with the $countries (above), I realised that PHP also misses some other crucial functions for word processing. These are basic word functions which I am used to having in a powerful language. Therefore, request changed to request words(), word(), wordindex(), wordlength() functions. Hugh Prior Previous Comments: ------------------------------------------------------------------------ [2002-04-19 04:28:16] [EMAIL PROTECTED] Derek, This function is EXTREMELY useful. The words() function is to strings what the sizeof() function is to arrays! I cannot imagine living without the sizeof() function when dealing with arrays; if I were dealing with strings a lot I cannot imagine living without a words() function. Here's some example code which shows how elegant it can make string use: /**************************/ $countries = "England Spain France Italy"; print "There are " . words($countries) . " countries competing"; print "The " . words($countries) . " are:"; for ($i=0; $i<=words($countries); $i++) { print word($countries, $i); } /***************************/ Of course, there are many other uses, but this I hope gives a flavour for how handy and elegant such functionality is. ------------------------------------------------------------------------ [2002-04-19 04:22:01] [EMAIL PROTECTED] 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. ------------------------------------------------------------------------ [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)); } ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/16681 -- Edit this bug report at http://bugs.php.net/?id=16681&edit=1