In PHP website development, we often need to split a string into an array. At this time, we often use the PHPexplode () function to help us implement it. In this article, we will mainly learn how explode () functions work. File 1: extstandardstring. c (source code of explode) PHP_FUNCTION (explode) {char
In PHP website development, we often need to split a string into an array. At this time, we often use the PHP explode () function to help us implement it. In this article, we will mainly learn how explode () functions work. File 1: ext/standard/string. c (source code of explode) PHP_FUNCTION (explode) {char
In PHP website development, we often need to split a string into an array. At this time, we often use the PHP explode () function to help us implement it. In this article, we will mainly learn how explode () functions work.
File 1: ext/standard/string. c (source code of explode)
PHP_FUNCTION (explode) {char * str, * delim; int str_len = 0, delim_len = 0; long limit = LONG_MAX;/* No limit */zval zdelim, zstr; if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "ss | l", & delim, & delim_len, & str, & str_len, & limit) = FAILURE) {return ;} if (delim_len = 0) {php_error_docref (NULL TSRMLS_CC, E_WARNING, "Empty delimiter"); RETURN_FALSE;} // an array is opened here, used to store the split data array_init (return_value); // because of this, we use explod E ('|', ''); becomes a valid if (str_len = 0) {if (limit> = 0) {add_next_index_stringl (return_value ,"", sizeof ("")-);} return;} // both of the following constructs the original string and delimiter into the _ zval_struct structure. // ZVAL_STRINGL will allocate space ~~ The source code is then pasted with ZVAL_STRINGL (& zstr, str, str_len, 0); ZVAL_STRINGL (& zdelim, delim, delim_len, 0 ); // The limit value is the third parameter of the explode that can be passed in explode. It allows positive and negative if (limit> 1) {php_explode (& zdelim, & zstr, return_value, limit );} elseif (limit <0) {limit (& zdelim, & zstr, return_value, limit);} else {add_index_stringl (return_value, 0, str, str_len, 1 );}}
This article is excerpted from PHP explode () function source code analysis.