iliaa           Wed Oct 16 23:27:19 2002 EDT

  Modified files:              
    /php4/ext/standard  basic_functions.c php_string.h string.c 
  Log:
  Added word_count() function that allows counting of words inside a string.
  The function also allows the user to retrieve all the words from a string.
  
  
Index: php4/ext/standard/basic_functions.c
diff -u php4/ext/standard/basic_functions.c:1.528 
php4/ext/standard/basic_functions.c:1.529
--- php4/ext/standard/basic_functions.c:1.528   Sun Oct  6 13:04:10 2002
+++ php4/ext/standard/basic_functions.c Wed Oct 16 23:27:19 2002
@@ -17,7 +17,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: basic_functions.c,v 1.528 2002/10/06 17:04:10 rasmus Exp $ */
+/* $Id: basic_functions.c,v 1.529 2002/10/17 03:27:19 iliaa Exp $ */
 
 #include "php.h"
 #include "php_streams.h"
@@ -334,6 +334,7 @@
        PHP_FE(stristr,                                                                
                                                 NULL)
        PHP_FE(strrchr,                                                                
                                                 NULL)
        PHP_FE(str_shuffle,                                                            
                                                         NULL)
+       PHP_FE(word_count,                                                             
+                                                 NULL)
 
 #ifdef HAVE_STRCOLL
        PHP_FE(strcoll,                                                                
                                                 NULL)
Index: php4/ext/standard/php_string.h
diff -u php4/ext/standard/php_string.h:1.63 php4/ext/standard/php_string.h:1.64
--- php4/ext/standard/php_string.h:1.63 Fri Oct 11 10:48:25 2002
+++ php4/ext/standard/php_string.h      Wed Oct 16 23:27:19 2002
@@ -17,7 +17,7 @@
    +----------------------------------------------------------------------+
 */
 
-/* $Id: php_string.h,v 1.63 2002/10/11 14:48:25 iliaa Exp $ */
+/* $Id: php_string.h,v 1.64 2002/10/17 03:27:19 iliaa Exp $ */
 
 /* Synced with php 3.0 revision 1.43 1999-06-16 [ssb] */
 
@@ -83,6 +83,7 @@
 PHP_FUNCTION(str_pad);
 PHP_FUNCTION(sscanf);
 PHP_FUNCTION(str_shuffle);
+PHP_FUNCTION(word_count);
 #ifdef HAVE_STRCOLL
 PHP_FUNCTION(strcoll);
 #endif
Index: php4/ext/standard/string.c
diff -u php4/ext/standard/string.c:1.323 php4/ext/standard/string.c:1.324
--- php4/ext/standard/string.c:1.323    Fri Oct 11 08:42:01 2002
+++ php4/ext/standard/string.c  Wed Oct 16 23:27:19 2002
@@ -18,7 +18,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: string.c,v 1.323 2002/10/11 12:42:01 sander Exp $ */
+/* $Id: string.c,v 1.324 2002/10/17 03:27:19 iliaa Exp $ */
 
 /* Synced with php 3.0 revision 1.193 1999-06-16 [ssb] */
 
@@ -4027,6 +4027,80 @@
 }
 /* }}} */
 
+/* {{{ proto void word_count(string str, [int format])
+       Counts the number of words inside a string. If format of 1 is specified,
+       then the function will return an array containing all the words
+       found inside the string. If format of 2 is specified, then the function
+       will return an associated array where the position of the word is the key
+       and the word itself is the value.
+       
+       For the purpose of this function, 'word' is defined as a locale dependent
+       string containing alphabetic characters, which also may contain, but not start
+       with "'" and "-" characters.
+*/
+PHP_FUNCTION(word_count)
+{
+       zval **str, **o_format;
+       char *s, *e, *p, *buf;
+       int word_count = 0;
+       int type = 0;
+       int n_args = ZEND_NUM_ARGS();
+
+       if( n_args > 2 || n_args < 1 || zend_get_parameters_ex(n_args, &str, 
+&o_format) == FAILURE) {
+               WRONG_PARAM_COUNT;
+       }
+       
+       if (n_args == 2) {
+               convert_to_long_ex(o_format);
+               type = Z_LVAL_PP(o_format);
+               
+               if (type != 1 && type != 2) {
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "The specified 
+format parameter, '%d' is invalid.", type);
+                       RETURN_FALSE;
+               }
+       }
+
+       convert_to_string_ex(str);
+       
+       p = s = Z_STRVAL_PP(str);
+       e = Z_STRVAL_PP(str) + Z_STRLEN_PP(str);
+               
+       if (type == 1 || type == 2) {
+               array_init(return_value);
+       }
+       
+       while (p < e) {
+               if (isalpha(*p++)) {
+                       s = p - 1;
+                       while (isalpha(*p) || *p == '\'' || (*p == '-' && 
+isalpha(*(p+1)))) {
+                               p++;
+                       }
+                       
+                       switch (type)
+                       {
+                               case 1:
+                                       buf = estrndup(s, (p-s));
+                                       add_next_index_stringl(return_value, buf, 
+(p-s), 1);
+                                       efree(buf);
+                                       break;
+                               case 2:
+                                       buf = estrndup(s, (p-s));
+                                       add_index_stringl(return_value, (s - 
+Z_STRVAL_PP(str)), buf, p-s, 1);
+                                       efree(buf);
+                                       break;
+                               default:
+                                       word_count++;
+                                       break;          
+                       }
+               }       
+       }
+       
+       if (!type) {
+               RETURN_LONG(word_count);                
+       }
+}
+
+/* }}} */
 
 #if HAVE_STRFMON
 /* {{{ proto string money_format(string format , float value)



-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to