]> granicus.if.org Git - php/commitdiff
Added word_count() function that allows counting of words inside a string.
authorIlia Alshanetsky <iliaa@php.net>
Thu, 17 Oct 2002 03:27:19 +0000 (03:27 +0000)
committerIlia Alshanetsky <iliaa@php.net>
Thu, 17 Oct 2002 03:27:19 +0000 (03:27 +0000)
The function also allows the user to retrieve all the words from a string.

ext/standard/basic_functions.c
ext/standard/php_string.h
ext/standard/string.c

index 311758adf9e6d6ab7d5a1eae4218baaca4c3d484..ee1200b38436e7b089c0b03628c254409436a33b 100644 (file)
@@ -334,6 +334,7 @@ function_entry basic_functions[] = {
        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 734bf176275730670b2be96720fa330539065e67..0acb3f654349eb7fb0daab41b9faa0823e04aa4b 100644 (file)
@@ -83,6 +83,7 @@ PHP_FUNCTION(substr_count);
 PHP_FUNCTION(str_pad);
 PHP_FUNCTION(sscanf);
 PHP_FUNCTION(str_shuffle);
+PHP_FUNCTION(word_count);
 #ifdef HAVE_STRCOLL
 PHP_FUNCTION(strcoll);
 #endif
index bb6f7a47e5e94a5ce19646cd916fe4004e62ab70..52bf54dbbedd453ff5369d08e4a0d1515edbf64c 100644 (file)
@@ -4027,6 +4027,80 @@ PHP_FUNCTION(str_shuffle)
 }
 /* }}} */
 
+/* {{{ 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)