From f15c24d44c57d9296277d0646df32e772d760be5 Mon Sep 17 00:00:00 2001 From: Andrey Hristov Date: Sat, 12 Mar 2005 14:41:20 +0000 Subject: [PATCH] added an optional third parameter to str_word_count(), which is used as a list of characters which are considerd "is_alpha()". Thus V14GR4 can be extracted as one word if the user supplies "14" as charlist. FR #31560 # Nuno, are you wishing to document this one too? :) --- ext/standard/string.c | 44 ++-- .../tests/strings/str_word_count.phpt | 212 +++++++++++++++++- 2 files changed, 236 insertions(+), 20 deletions(-) diff --git a/ext/standard/string.c b/ext/standard/string.c index 4052cf0b13..7d0df42e04 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -4643,7 +4643,7 @@ PHP_FUNCTION(str_shuffle) } /* }}} */ -/* {{{ proto mixed str_word_count(string str, [int format]) +/* {{{ proto mixed str_word_count(string str, [int format [, string charlist]]) 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 @@ -4656,38 +4656,44 @@ PHP_FUNCTION(str_shuffle) */ PHP_FUNCTION(str_word_count) { - zval **str, **o_format; + zval **str, **o_format, **also_alphas; char *s, *e, *p, *buf; + char *p_also = NULL; 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) { + if (n_args > 3 || n_args < 1 || zend_get_parameters_ex(n_args, &str, &o_format, &also_alphas) == FAILURE) { WRONG_PARAM_COUNT; } - - if (n_args == 2) { - convert_to_long_ex(o_format); - type = Z_LVAL_PP(o_format); + switch (n_args) { + case 3: + convert_to_string_ex(also_alphas); + if (Z_STRLEN_PP(also_alphas)) { + p_also = Z_STRVAL_PP(also_alphas); + } + case 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; - } + if (type != 1 && type !=2 && !(n_args >= 3 && type == 0)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "The specified format parameter, '%d' is invalid.", type); + RETURN_FALSE; + } + if (type) { + array_init(return_value); + } } convert_to_string_ex(str); - e = Z_STRLEN_PP(str) + (p = s = Z_STRVAL_PP(str)); - - if (type) { - array_init(return_value); - } - while (p < e) { if (isalpha(*p++)) { - s = p - 1; - while (isalpha(*p) || *p == '\'' || (*p == '-' && isalpha(*(p+1)))) { + s = p - 1; /* the word starts at s */ + while (isalpha(*p) || *p == '\'' || (*p == '-' && isalpha(*(p+1))) + || (p_also && php_memnstr(p_also, p, 1, p_also + Z_STRLEN_PP(also_alphas))) + ) + { p++; } diff --git a/ext/standard/tests/strings/str_word_count.phpt b/ext/standard/tests/strings/str_word_count.phpt index 4c9c0780f6..aa5eb9e87c 100644 --- a/ext/standard/tests/strings/str_word_count.phpt +++ b/ext/standard/tests/strings/str_word_count.phpt @@ -17,6 +17,26 @@ var_dump(str_word_count($str, array())); var_dump(str_word_count($str, $b)); var_dump($str); +$str2 = "F0o B4r 1s bar foo"; +var_dump(str_word_count($str2, NULL, "04")); +var_dump(str_word_count($str2, NULL, "01")); +var_dump(str_word_count($str2, NULL, "014")); +var_dump(str_word_count($str2, NULL, array())); +var_dump(str_word_count($str2, NULL, new stdClass)); +var_dump(str_word_count($str2, NULL, "")); +var_dump(str_word_count($str2, 1, "04")); +var_dump(str_word_count($str2, 1, "01")); +var_dump(str_word_count($str2, 1, "014")); +var_dump(str_word_count($str2, 1, array())); +var_dump(str_word_count($str2, 1, new stdClass)); +var_dump(str_word_count($str2, 1, "")); +var_dump(str_word_count($str2, 2, "04")); +var_dump(str_word_count($str2, 2, "01")); +var_dump(str_word_count($str2, 2, "014")); +var_dump(str_word_count($str2, 2, array())); +var_dump(str_word_count($str2, 2, new stdClass)); +var_dump(str_word_count($str2, 2, "")); + ?> --EXPECTF-- array(6) { @@ -67,4 +87,194 @@ bool(false) Warning: str_word_count(): The specified format parameter, '0' is invalid. in %s on line 14 bool(false) string(55) "Hello friend, you're - looking good today!" \ No newline at end of file + looking good today!" +int(5) +int(6) +int(5) + +Notice: Array to string conversion in %s on line 21 +int(7) + +Notice: Object of class stdClass to string conversion in %s on line 22 +int(7) +int(7) +array(5) { + [0]=> + string(3) "F0o" + [1]=> + string(3) "B4r" + [2]=> + string(1) "s" + [3]=> + string(3) "bar" + [4]=> + string(3) "foo" +} +array(6) { + [0]=> + string(3) "F0o" + [1]=> + string(1) "B" + [2]=> + string(1) "r" + [3]=> + string(1) "s" + [4]=> + string(3) "bar" + [5]=> + string(3) "foo" +} +array(5) { + [0]=> + string(3) "F0o" + [1]=> + string(3) "B4r" + [2]=> + string(1) "s" + [3]=> + string(3) "bar" + [4]=> + string(3) "foo" +} + +Notice: Array to string conversion in %s on line 27 +array(7) { + [0]=> + string(1) "F" + [1]=> + string(1) "o" + [2]=> + string(1) "B" + [3]=> + string(1) "r" + [4]=> + string(1) "s" + [5]=> + string(3) "bar" + [6]=> + string(3) "foo" +} + +Notice: Object of class stdClass to string conversion in %s on line 28 +array(7) { + [0]=> + string(1) "F" + [1]=> + string(1) "o" + [2]=> + string(1) "B" + [3]=> + string(1) "r" + [4]=> + string(1) "s" + [5]=> + string(3) "bar" + [6]=> + string(3) "foo" +} +array(7) { + [0]=> + string(1) "F" + [1]=> + string(1) "o" + [2]=> + string(1) "B" + [3]=> + string(1) "r" + [4]=> + string(1) "s" + [5]=> + string(3) "bar" + [6]=> + string(3) "foo" +} +array(5) { + [0]=> + string(3) "F0o" + [4]=> + string(3) "B4r" + [9]=> + string(1) "s" + [11]=> + string(3) "bar" + [15]=> + string(3) "foo" +} +array(6) { + [0]=> + string(3) "F0o" + [4]=> + string(1) "B" + [6]=> + string(1) "r" + [9]=> + string(1) "s" + [11]=> + string(3) "bar" + [15]=> + string(3) "foo" +} +array(5) { + [0]=> + string(3) "F0o" + [4]=> + string(3) "B4r" + [9]=> + string(1) "s" + [11]=> + string(3) "bar" + [15]=> + string(3) "foo" +} + +Notice: Array to string conversion in %s on line 33 +array(7) { + [0]=> + string(1) "F" + [2]=> + string(1) "o" + [4]=> + string(1) "B" + [6]=> + string(1) "r" + [9]=> + string(1) "s" + [11]=> + string(3) "bar" + [15]=> + string(3) "foo" +} + +Notice: Object of class stdClass to string conversion in %s on line 34 +array(7) { + [0]=> + string(1) "F" + [2]=> + string(1) "o" + [4]=> + string(1) "B" + [6]=> + string(1) "r" + [9]=> + string(1) "s" + [11]=> + string(3) "bar" + [15]=> + string(3) "foo" +} +array(7) { + [0]=> + string(1) "F" + [2]=> + string(1) "o" + [4]=> + string(1) "B" + [6]=> + string(1) "r" + [9]=> + string(1) "s" + [11]=> + string(3) "bar" + [15]=> + string(3) "foo" +} \ No newline at end of file -- 2.50.1