}
/* }}} */
-/* {{{ 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
*/
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++;
}
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) {
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