diff options
author | Ilia Alshanetsky <iliaa@php.net> | 2002-10-17 03:27:19 +0000 |
---|---|---|
committer | Ilia Alshanetsky <iliaa@php.net> | 2002-10-17 03:27:19 +0000 |
commit | 47b57f256995302ab968b4c94252dd22283fe0d0 (patch) | |
tree | 8b14cac21dc65496789867a63655ae64697efb6f | |
parent | 8aaa004980d00f1b01ea62ed9663a59995a24e85 (diff) | |
download | php-git-47b57f256995302ab968b4c94252dd22283fe0d0.tar.gz |
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.
-rw-r--r-- | ext/standard/basic_functions.c | 1 | ||||
-rw-r--r-- | ext/standard/php_string.h | 1 | ||||
-rw-r--r-- | ext/standard/string.c | 74 |
3 files changed, 76 insertions, 0 deletions
diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 311758adf9..ee1200b384 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -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) diff --git a/ext/standard/php_string.h b/ext/standard/php_string.h index 734bf17627..0acb3f6543 100644 --- a/ext/standard/php_string.h +++ b/ext/standard/php_string.h @@ -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 diff --git a/ext/standard/string.c b/ext/standard/string.c index bb6f7a47e5..52bf54dbbe 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -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) |