php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #3251 3rd arg to explode, (same 3rd arg that split() has)
Submitted: 2000-01-18 21:51 UTC Modified: 2000-05-23 02:23 UTC
From: tom at zworg dot com Assigned: hholzgra (profile)
Status: Closed Package: Feature/Change Request
PHP Version: 4.0 Beta 3 OS:
Private report: No CVE-ID: None
 [2000-01-18 21:51 UTC] tom at zworg dot com
how about an optional 3rd arg 'limit' for explode()?  like split() has

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-05-19 19:08 UTC] hholzgra at cvs dot php dot net
done
 [2000-05-19 19:10 UTC] hholzgra at cvs dot php dot net
... and closed
 [2000-05-20 02:23 UTC] hholzgra at cvs dot php dot net
suspended 'till after release

patch:

--- ext/standard/php_string.h	2000/04/26 00:29:59	1.12
+++ ext/standard/php_string.h	2000/05/19 22:00:38	1.13
@@ -109,7 +109,7 @@
 PHPAPI void php_char_to_str(char *str, uint len, char from, char *to, int to_len, pval *result);
 
 PHPAPI void php_implode(pval *delim, pval *arr, pval *return_value);
-PHPAPI void php_explode(pval *delim, pval *str, pval *return_value);
+PHPAPI void php_explode(pval *delim, pval *str, pval *return_value, int limit);
 PHPAPI inline char *php_memnstr(char *haystack, char *needle, int needle_len, char *end);
 PHPAPI size_t php_strspn(char *s1, char *s2, char *s1_end, char *s2_end); 
 PHPAPI size_t php_strcspn(char *s1, char *s2, char *s1_end, char *s2_end); 

--- ext/standard/string.c	2000/05/18 15:34:35	1.110
+++ ext/standard/string.c	2000/05/19 22:00:38	1.111
@@ -213,7 +213,7 @@
 }
 /* }}} */
 
-PHPAPI void php_explode(zval *delim, zval *str, zval *return_value) 
+PHPAPI void php_explode(zval *delim, zval *str, zval *return_value, int limit) 
 {
 	char *p1, *p2, *endp;
 	int i = 0;
@@ -229,22 +229,37 @@
 		do {
 			add_index_stringl(return_value, i++, p1, p2-p1, 1);
 			p1 = p2 + delim->value.str.len;
+			if((limit>=0)&&(i>=limit-1))
+				break;
 		} while ((p2 = php_memnstr(p1, delim->value.str.val, delim->value.str.len, endp)) != NULL);
 
-		if (p1 <= endp) {
+		if ((p1 <= endp)|| ((limit>=0)&&(i>=limit-1))){
 			add_index_stringl(return_value, i++, p1, endp-p1, 1);
 		}
 	}
 }
 
-/* {{{ proto array explode(string separator, string str)
+/* {{{ proto array explode(string separator, string str [, int limit])
    Split a string on string separator and return array of components */
 PHP_FUNCTION(explode)
 {
-	zval **str, **delim;
+	zval **str, **delim, **zlimit = NULL;
+	int limit;
 
-	if (ARG_COUNT(ht) != 2 || zend_get_parameters_ex(2, &delim, &str) == FAILURE) {
+	switch (ARG_COUNT(ht)) {
+	case 2:
+		if (zend_get_parameters_ex(2, &delim, &str) == FAILURE)
+			WRONG_PARAM_COUNT;
+		limit=-1;
+		break;
+	case 3:
+		if (zend_get_parameters_ex(3, &delim, &str, &zlimit) == FAILURE)
 		WRONG_PARAM_COUNT;
+		convert_to_long_ex(zlimit);
+		limit = (*zlimit)->value.lval;
+		break;
+	default:
+		WRONG_PARAM_COUNT;                                         
 	}
 
 	convert_to_string_ex(str);
@@ -259,7 +274,11 @@
 		RETURN_FALSE;
 	}
 
-	php_explode(*delim, *str, return_value);
+	if((limit==0)||(limit==1)) {
+		add_index_stringl(return_value, 0, (*str)->value.str.val, (*str)->value.str.len, 1);
+	} else {
+		php_explode(*delim, *str, return_value, limit);
+	}
 }
 /* }}} */


 [2000-05-23 02:23 UTC] hholzgra at cvs dot php dot net
now its in
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Oct 08 09:01:27 2024 UTC