On PHP source one: explode and implode functions

Source: Internet
Author: User
This article mainly introduces about PHP source one: Explode and implode functions, has a certain reference value, now share to everyone, have the need for friends can refer to

First, explode and implode functions
Array explode (string separator, string string [, int limit])
This function returns an array of strings, each of which is a substring of string, separated by the separator of the strings as the boundary points. If the limit parameter is set, the returned array contains a maximum of limit elements, and the last element will contain the remainder of the string.

The time complexity of this function should be O (strlen (separator) * strlen (String))
The implementation process is basically traversing string strings, comparing it to separator, if the same, writing the hash table and moving the pointer of string to the new position (that is, to the right of each separator);

In addition, special handling is available for cases with limit less than 0
This function implementation is mainly dependent on the PHP_MEMNSTR function, in the Php.h file we can see its definition,
#define PHP_MEMNSTR Zend_memnstr
Its real function is Zend_memnstr, in the Zend/zend_operators.h file of 217 lines, you can see its definition, its implementation is mainly a while loop and two C language functions MEMCHR and MEMCMP

String implode (string glue, array pieces)
This function returns a string of elements of the pieces array concatenated with the glue string.
This function can be an array parameter, can be an array and a string parameter, and the sequence of strings and arrays can be changed, these in the program has a special treatment for each case, the following code:

if (argc = = 1) {if (Z_TYPE_PP (arg1)! = Is_array) {// There is only one parameter and is not yet an array php_error_docref (NULL tsrmls_cc, e_warning, "Argument must is an AR                                          Ray ");                            Return } make_std_zval (Delim); #define _IMPL_EMPTY "" Zval_stringl (Delim, _                             Impl_empty, sizeof (_impl_empty)-1, 0);                            Separate_zval (ARG1);              arr = *arg1;              } else {//two parameters if (z_type_pp (arg1) = = Is_array) {//                                          If each parameter is an array of arr = *ARG1;                                          CONVERT_TO_STRING_EX (ARG2);                            Delim = *ARG2; } else if (z_type_pp (arg2) = = Is_array) {//              If the second parameter is an array of arr = *arg2;                                          CONVERT_TO_STRING_EX (ARG1);                            Delim = *ARG1;                                           } else {php_error_docref (NULL tsrmls_cc, e_warning, "Invalid arguments passed");                            Return }              }

The last array is assigned to arr, and the delimited string is assigned to Delim, without the ""

Is the process of iterating through an array and connecting strings, except that the smart_str correlation function is used in this process (more related please move), different connection operations are made for different types (if numbers also need to be converted into strings, these are related functions in smart_str)

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Related Article

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.