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!