"PHP source reading" Explode and implode functions, Explodeimplode
The explode and implode functions are primarily used as string and array conversions, such as splitting a string based on a character after acquiring a parameter, or using one word to match the result of an array and outputting it as a string. These two functions are often used in PHP, so it is necessary to understand their rationale.
Explode
Array explode (string $delimiter, String $string, [, $limit])
The function returns an array of strings, each of which is a substring of string, separated by the $delimiter as a boundary point.
Parameter description
Limit
If $limit is set and is a positive number, the returned array contains a maximum of $limit elements, and the last element will contain the remainder of the $string.
If $limit is a negative number, all elements except the last-$limit element are returned.
If $limit is 0, it will be treated as 1.
Delimiter
If $delimiter is empty, the function returns FALSE. If delimiter is not in a string and $limit is negative, an empty array is returned.
Run the sample
$str = ' hello,world,heiheihei,php ';
Let's take a look at the case where limit is not set
$arr Explode $str ); Print_r ($arr);
When limit is positive, limit is set to 1 and returns up to 1 elements.
$arr Explode $str, 1); Print_r ($arr);
Limit is a negative number, and limit is-1, which returns all elements outside the last 1 elements.
$arr Explode $str,-1); Print_r ($arr);
Limit is 0, treated as 1.
$arr Explode $str, 0); Print_r ($arr);
Explode execution steps
1, receive parameters, processing parameters are empty cases
2. Creating local variables used in functions
3. Calling different functions to separate strings according to the value of limit
The core implementation of the EXPLODE function is the Php_explode function, the following is the execution flow diagram of the function:
if (P2 = = NULL) { // cannot find delimiter, directly returns entire string Add_next_index_stringl (Return_value, p1, Z_strlen_p (str) , 1); } Else { do { /// P1 is added 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); // adds the last value to Return_value if (P1 <= ENDP) Add_next_index_stringl (return_ Value, p1, ENDP-p1, 1); }
SOURCE Interpretation
sizeof ("") = = 0. sizeof has two usages,sizeof (TypeName) and sizeof (expression), when the argument is TypeName is, that is, the type name, sizeof returns the size of the object corresponding to the type When the argument is an expression, sizeof evaluates the return type of the expression corresponding to the size of the object. Here, "" is the expression, sizeof calculates the space that the compiler assigns to "" at compile time, which is calculated as the length of the strlen, and therefore is 1, and the function does not calculate the.
If you do not set Limit,limit, the default value is Long_max. In the Php.h file, Long_max is defined as 2147483647L.
In the implementation, if limit is greater than 1, the php_explode function is called, and if limit is less than 0, the php_explode_negative_limit function is called and if limit equals 0, is treated as 1, the add_index_stringl function is called to add STR to the array return_value.
The php_memnstr function is called when the delimiter delimiter is found
php_memnstr (z_strval_p (str), z_strval_p (Delim), Z_strlen_p (Delim), ENDP);
And PHP_MEMNSTR is the macro definition of zend_memnstr , ZEND_MEMNSTR implementation inside, so actually called the C inside the MEMCHR to find the character delimiter.
Once the delimiter location is found, the Add_next_index_stringl function is called to insert the delimited string 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 argument is empty, the second parameter is the default value '. This function can be thought of as a reverse process of explode.
Of course, using the order in which the documents are prescribed avoids confusion.
Run the sample
$arr Array (' Hello ', ' world ');
Follow Document order parameters
$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 empty, then determine whether the type of the first parameter is an array, if not, then an error. Otherwise, use "" To assign a value to the glue, using it as the connector.
3, if the second argument is not empty, then if the first argument is an array type, the second argument is converted to a string type; otherwise, if the second argument is an array type, the first argument is converted to a string type.
4. Call the Php_implode function to make a string connection.
After the implode function has set the parameters, the underlying calls the Php_implode function for string connections, and the Php_implode function executes the following flowchart:
//iterate through each element of the array, determine its type, and then 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) { CaseIs_string:smart_str_appendl (&implstr, Z_STRVAL_PP (TMP), Z_STRLEN_PP (TMP)); Break; CaseIs_long: {CharStmp[max_length_of_long +1]; Str_len= slprintf (Stmp,sizeof(STMP),"%ld", Z_LVAL_PP (TMP)); Smart_str_appendl (&implstr, Stmp, Str_len); } Break; CaseIs_bool:if(Z_LVAL_PP (tmp) = =1) {Smart_str_appendl (&IMPLSTR,"1",sizeof("1")-1); } Break; CaseIs_null: Break; Caseis_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; CaseIs_object: {intcopy; 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; Zval_copy_ctor (&tmp_val); Convert_to_string (&tmp_val); Smart_str_appendl (&Implstr, Z_strval (Tmp_val), Z_strlen (Tmp_val)); Zval_dtor (&tmp_val); Break; } //Add glue characters if(++i! =Numelems) {Smart_str_appendl (&Implstr, Z_strval_p (Delim), Z_strlen_p (Delim)); } zend_hash_move_forward_ex (Z_arrval_p (arr),&POS); } //add end character at tail 0Smart_str_0 (&IMPLSTR);
SOURCE Interpretation
Php_implode will retrieve the contents of the array one by one, then determine the type of each element, and after making the necessary data type conversions, call the Smart_str_appendl function to append the value to the returned string. Finally, add a terminator to the string, which is a necessary operation and should be noted later in programming.
Smart_str_appendl is a macro definition of the function SMART_STR_APPENDL_EX that calls the memcpy to copy the string.
Summary
Original article, writing Limited, Caishuxueqian, if there is not in the text, million hope to inform.
For the time being write so much, there are more optimization and PHP source commonly used in the function, will be in the future source reading slowly tell.
If this article is helpful to you, please click on the recommendation, thank you ^_^
Finally, I have a more detailed comment on the PHP source code on GitHub. Interested can be onlookers, to a star. PHP5.4 source annotation.
http://www.bkjia.com/PHPjc/1128118.html www.bkjia.com true http://www.bkjia.com/PHPjc/1128118.html techarticle "PHP source reading" Explode and implode functions, explodeimplode explode and implode functions are mainly used for the operation of the conversion between strings and arrays, such as to get a parameter after a certain character ...