PHP source code reading: explode, implode functions, explode and implode functions are mainly used for conversion between strings and arrays. for example, after obtaining a parameter, a character is used to separate the string, or combine the results of an array with one character into a string for output. These two functions are often used in PHP, so it is necessary to understand their principles.
Explode
array explode ( string $delimiter, string $string, [ , $limit ] )
The function returns an array composed of strings. each element is a substring of a string, which is separated by the string $ delimiter as the boundary point.
Parameter description
Limit
If limit is set and it is a positive number, the returned array can contain up to limit elements, and the last element will contain the rest of the string.
If the limit value is negative, all elements except the last-$ limit element are returned.
If the limit value is 0, it is regarded as 1.
Delimiter
If delimiter is empty, the function returns FALSE. If delimiter is not in string and limit is negative, an empty array is returned.
Running example
$str = 'hello,world,heiheihei,php';
Let's see if limit is not set.
$arr = explode(',', $str);print_r($arr);
When limit is a positive number, limit is set to 1 and a maximum of 1 elements are returned.
$arr = explode(',', $str, 1);print_r($arr);
Limit is a negative number, and limit is-1. all elements outside the last element are returned.
$arr = explode(',', $str, -1);print_r($arr);
Limit is 0 and treated as 1.
$arr = explode(',', $str, 0);print_r($arr);
Explode execution steps
1. receive parameters. if the processing parameter is null
2. create local variables used in functions
3. call different functions to separate strings based on the limit value
The core implementation of the explode function is the php_explode function. the execution flowchart of this function is as follows:
Core code of php_explode function:
If (p2 = NULL) {// if no separator is found, the entire string add_next_index_stringl (return_value, p1, Z_STRLEN_P (str), 1) is returned directly );} else {do {// add p1 to the return_value array add_next_index_stringl (return_value, p1, p2-p1, 1); p1 = p2 + Z_STRLEN_P (delim );} while (p2 = php_memnstr (p1, Z_STRVAL_P (delim), Z_STRLEN_P (delim), endp ))! = NULL & -- limit> 1); // add the last value to return_value if (p1 <= endp) add_next_index_stringl (return_value, p1, endp-p1, 1 );}
Source code explanation
Sizeof ("") = 0. Sizeof has two usage methods,Sizeof (typename)AndSizeof (expression)When the parameter is typename, that is, the type name, sizeof, the size of the object corresponding to the returned type; when the parameter is an expression, sizeof calculates the size of the object corresponding to the returned type of the expression. Here, "" is an expression. sizeof refers to the space allocated by the compiler to "" during computation. in this case, the length must be calculated. Therefore, it is 1, and the strlen function will not calculate it.
If limit is not set, the default value of limit isLONG_MAX. In the php. h file, LONG_MAX is defined as 2147483647L.
In the implementation, if the limit is greater than 1, callPhp_explodeFunction. if the limit value is less than 0, it is called.Php_explode_negative_limitFunction; if limit is equal to 0, it is treated as 1.Add_index_stringlThe function adds str to the array return_value.
The delimiterPhp_memnstrFunction php_memnstr (Z_STRVAL_P (str), Z_STRVAL_P (delim), Z_STRLEN_P (delim), endp); and php_memnstr isZend_memnstrZend_memnstr implements the macro definition. Therefore, memchr in C is called to find the character delimiter.
CallAdd_next_index_stringlThe function inserts the separated strings into the returned array.
Implode
string implode ( string $glue, array $pieces )string implode ( array $pieces )
Converts the value of a one-dimensional array to a string.
Parameter description
The implode function can receive two parameter sequences. In addition, if the first parameter is an array and the second parameter is null, the second parameter defaults ''. This function can be seen as a reverse process of explode.
Of course, use the sequence specified in the document to avoid confusion.
Running example
$arr = array('hello', 'world');
Parameters in document order
$ Str = implode ('-', $ arr); // output "hello-world"
The first parameter is an array.
$ Str = implode ($ arr); // output "helloworld" $ str = implode ($ arr, '-'); // output "hello-world"
Implode execution steps
1. receive parameters and assign values
2. if the second parameter is null, the system checks whether the type of the first parameter is an array. if not, an error is returned. Otherwise, use "" to assign a value to glue and use it as the connector.
3. if the second parameter is not null, the second parameter is converted to the string type if the first parameter is of the array type. Otherwise, if the second parameter is of the array type, the first parameter is converted to the string type.
4. call the php_implode function to connect strings.
After the implode function sets parameters, the underlying layer calls the php_implode function for string connection. the execution flow of the php_implode function is as follows:
Core code of php_implode function:
// Traverse every element of the array, determine its type, and call the smart_str_appendl function to append the value to the string while (zend_hash_get_current_data_ex (Z_ARRVAL_P (arr), (void **) & tmp, & pos) = SUCCESS) {switch (* tmp)-> type) {case IS_STRING: smart_str_appendl (& implstr, Z_STRVAL_PP (tmp), Z_STRLEN_PP (tmp); break; case IS_LONG: {char stmp [limit + 1]; str_len = slprintf (stmp, sizeof (stmp), "% ld", Z_LVAL_PP (tmp); smart_str_appendl (& implstr, stmp, str_len);} break; case IS_BOOL: if (Z_LVAL_PP (tmp) = 1) {smart_str_appendl (& implstr, "1", sizeof ("1 ") -1);} break; case IS_NULL: break; case IS_DOUBLE: {char * stmp; str_len = spprintf (& stmp, 0, "%. * G ", (int) EG (precision), Z_DVAL_PP (tmp); smart_str_appendl (& implstr, stmp, str_len); efree (stmp);} break; case IS_OBJECT: {int copy; zval expr; zend_make_printable_zval (* tmp, & expr,©); Smart_str_appendl (& implstr, Z_STRVAL (expr), Z_STRLEN (expr); if (copy) {zval_dtor (& expr) ;}} break; default: tmp_val = ** tmp; values (& tmp_val); convert_to_string (& tmp_val); values (& implstr, Z_STRVAL (tmp_val), Z_STRLEN (tmp_val); zval_dtor (& tmp_val ); break;} // add the glue character if (++ I! = Numelems) {smart_str_appendl (& implstr, Z_STRVAL_P (delim), Z_STRLEN_P (delim);} substring (Z_ARRVAL_P (arr), & pos );} // add the End character 0 smart_str_0 (& implstr) at the end );
Source code explanation
Php_implode gets the content in the array one by one, then judges the type of each element, and then calls the smart_str_appendl function to append the value to the return string after necessary data type conversion. Finally, you must add an Terminator to the end of the string. this is a required operation and should be noted later in programming.
Smart_str_appendl is the macro definition of the smart_str_appendl_ex function.MemcpyCopy strings.
Summary
For the time being, there are more optimizations and common functions in the PHP source code, which will be described in the future.
The original article is limited in writing, so it is easy to learn. if there is anything wrong with the article, please let us know.
If this article is helpful to you, please click here for recommendations. thank you ^_^
At last, I have a more detailed description of the PHP source code on github. If you are interested, you can look around and give a star. PHP5.4 source code annotation. You can use the commit record to view the added annotations.
I hope you can write more excellent articles. thank you!
Rewarded
I hope you can write more excellent articles. thank you!
Any payment method
About the Author: hoohack
An ongoing Cainiao personal homepage · My Articles · 9 ·