Home >Backend Development >PHP Tutorial >[PHP source code reading] explode and implode functions, explodeimplode_PHP tutorial
explode and implode functions are mainly used for conversion operations between strings and arrays, such as obtaining a parameter and then according to a certain Character splits the string, or combines the result of an array into a string output using one character. These two functions are often used in PHP, so it is necessary to understand their principles.
<p>array explode ( string $delimiter, string $string, [ , $limit ] )</p>
The function returns an array composed of strings. Each element is a substring of string, separated by the string $delimiter as a boundary point.
limit
If $limit is set and is a positive number, the returned array contains at most $limit elements, and the last element will contain the remainder of $string.
If $limit is a negative number, return all elements except the last -$limit elements.
If $limit is 0, it will be treated as 1.
delimiter
If $delimiter is empty, the function returns FALSE. If delimiter is not in string and $limit is a negative number, an empty array is returned.
<span>$str</span> = 'hello,world,heiheihei,php';
Let’s first look at the situation without setting a limit
<span>$arr</span> = <span>explode</span>(',', <span>$str</span><span>); </span><span>print_r</span>(<span>$arr</span>);
When limit is a positive number, limit is set to 1, and a maximum of 1 element is returned.
<span>$arr</span> = <span>explode</span>(',', <span>$str</span>, 1<span>); </span><span>print_r</span>(<span>$arr</span>);
limit is a negative number, limit is -1, and all elements except the last element are returned.
<span>$arr</span> = <span>explode</span>(',', <span>$str</span>, -1<span>); </span><span>print_r</span>(<span>$arr</span>);
limit is 0 and is treated as 1.
<span>$arr</span> = <span>explode</span>(',', <span>$str</span>, 0<span>); </span><span>print_r</span>(<span>$arr</span>);
<p>1、接收参数,处理参数为空的情况</p> <p>2、创建函数中使用的局部变量</p> <p>3、根据limit的值调用不同的函数分隔字符串</p>
The core implementation of the explode function is the php_explode function. The following is the execution flow chart of this function:
if (p2 == NULL) { // If the separator is not found, the entire string is returned directly add_next_index_stringl(return_value, p1, Z_STRLEN_P(str), 1); } 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 4d10ac5dd1b1ff198f4d1fc8bc409372type) { case IS_STRING: smart_str_appendl(&implstr, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp)); break; case IS_LONG: { char stmp[MAX_LENGTH_OF_LONG + 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; 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; } // 添加glue字符 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); } // 在尾部添加结束字符0 smart_str_0(&implstr);
php_implode会逐个获取数组里面的内容,然后判断每个元素的类型,再做必要的数据类型转换之后,调用smart_str_appendl函数将值追加到返回的字符串后面。最后,还要在字符串后面加上结束符,这是个必须的操作,以后编程时也应注意。
smart_str_appendl是函数smart_str_appendl_ex的宏定义,该函数调用了memcpy做字符串的复制。
原创文章,文笔有限,才疏学浅,文中若有不正之处,万望告知。
暂且写这么多吧,还有更多的优化和PHP源码中常用的函数,将会在以后的源码阅读中慢慢讲述。
如果本文对你有帮助,请点下推荐吧,谢谢^_^
最后,我在github有对PHP源码更详细的注解。感兴趣的可以围观一下,给个star。PHP5.4源码注解。