PHPexplode () function source code analysis

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.