]> granicus.if.org Git - php/commitdiff
Added str_split() function. This function can be used to break down a
authorIlia Alshanetsky <iliaa@php.net>
Fri, 7 Feb 2003 21:36:18 +0000 (21:36 +0000)
committerIlia Alshanetsky <iliaa@php.net>
Fri, 7 Feb 2003 21:36:18 +0000 (21:36 +0000)
string into an array.

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

index 855453764555e1f105d509af97425c83c11cd55a..3809dc145bbd68b61888dd268b684da926223ae3 100644 (file)
@@ -342,6 +342,7 @@ function_entry basic_functions[] = {
        PHP_FE(strrchr,                                                                                                                 NULL)
        PHP_FE(str_shuffle,                                                                                                                     NULL)
        PHP_FE(str_word_count,                                                                                                          NULL)
+       PHP_FE(str_split,                                                                                                               NULL)
 
 #ifdef HAVE_STRCOLL
        PHP_FE(strcoll,                                                                                                                 NULL)
index debe85ade09c463055956d233017738c7a050667..60a6c23af2bf93a69a9d0804629af8e5579c14c4 100644 (file)
@@ -87,6 +87,7 @@ PHP_FUNCTION(str_pad);
 PHP_FUNCTION(sscanf);
 PHP_FUNCTION(str_shuffle);
 PHP_FUNCTION(str_word_count);
+PHP_FUNCTION(str_split);
 #ifdef HAVE_STRCOLL
 PHP_FUNCTION(strcoll);
 #endif
index 02912b4bbd1d763e40605b7ec03fed17ea7135f0..fe2f8bfe23826e1558d2550c303181a82d546f19 100644 (file)
@@ -37,6 +37,9 @@
 #ifdef HAVE_MONETARY_H
 # include <monetary.h>
 #endif
+
+#include <math.h>
+
 #include "scanf.h"
 #include "zend_API.h"
 #include "zend_execute.h"
@@ -4296,10 +4299,44 @@ PHP_FUNCTION(money_format) {
 
        RETURN_STRINGL(erealloc(str, str_len + 1), str_len, 0);
 }
-
 /* }}} */
 #endif
 
+/* {{{ proto array str_split(string str [, int split_length])
+   Convert a string to an array. If split_length is specified, break the string down into chunks each split_length characters long. */
+PHP_FUNCTION(str_split) {
+       char *str;
+       int str_len;
+       long split_length = 1;
+       char *p;
+       int n_reg_segments;
+       
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &str, &str_len, &split_length) == FAILURE) {
+               return;
+       }
+
+       if (split_length <= 0) {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "The the length of each segment must be greater then zero.");
+               RETURN_FALSE;
+       }
+
+       array_init(return_value);
+
+       n_reg_segments = floor(str_len / split_length);
+       p = str;
+
+       while (n_reg_segments-- > 0) {
+               add_next_index_stringl(return_value, p, split_length, 1);
+               p += split_length;
+       }
+
+       if (p != (str + str_len)) {
+               add_next_index_stringl(return_value, p, (str + str_len - p), 1);
+       }
+}
+/* }}} */
+
+
 /*
  * Local variables:
  * tab-width: 4