In PHP, the explode() function is used to convert a string into an array. The explode() function can split a string based on the string separator, that is, split a string into several substrings based on the separator, and then combine these substrings into an array and return it; the syntax is "explode(separator, string , the number of array elements)".
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
In php, explode() Function is used to convert a string into an array.
explode() function uses one string to split another string and returns an array composed of strings.
In detail: The explode() function can split a string based on the string delimiter, that is, split a string into several substrings based on the delimiter, and then combine these substrings into an array and return it.
explode($delimiter, $string [, $limit])
The parameter description is as follows:
Note: The $delimiter parameter cannot be an empty string. Otherwise, an error will be reported.
<?php header('content-type:text/html;charset=utf-8'); $str = 'hypertext language programming'; var_dump($str); $arr=explode(" ",$str); var_dump($arr); $arr=explode("t",$str); var_dump($arr); $arr=explode("",$str); var_dump($arr); ?>
$limit takes different values:
<?php header('content-type:text/html;charset=utf-8'); $str = 'hypertext language programming'; var_dump($str); $arr=explode(" ",$str); var_dump($arr); $arr=explode(" ",$str,2); var_dump($arr); $arr=explode(" ",$str,-1); var_dump($arr); ?>
Recommended learning: "PHP Video Tutorial》
The above is the detailed content of What is php explode used to convert to an array?. For more information, please follow other related articles on the PHP Chinese website!