Home > Article > Backend Development > How to convert string to array in php?
In PHP, you can use the built-in function explode() to convert a string into an array; this function can split a string into several substrings based on delimiters, and then combine these substrings into an array And return, the syntax format is "explode (delimiter, string, return the number of array elements)".
#During the development process, we often encounter situations where we need to convert a string into an array. PHP has a built-in explode() function that can split a string into several parts according to specific delimiters.
Recommended: "PHP Video Tutorial"
explode() function can split a string based on the string delimiter, that is, it splits a string based on the delimiter. Divide it into several substrings, then combine these substrings into an array and return it. The syntax format is as follows:
explode(separator,string,limit)
The parameter description is as follows:
delimiter: the delimiter character used to split the string;
string: the string that needs to be split;
limit: optional parameter, which can be empty, specifies the number of array elements to be returned;
If limit is not empty and is a positive number, the returned array contains at most limit elements, and the last element contains the remaining part of string;
If limit is not If it is empty and negative, all elements except the last limit elements will be returned;
If limit is 0, it will be treated as 1;
If limit is empty, it means returning all array elements.
If delimiter is an empty string "
", the program will prompt a Warning, and the explode() function will return FALSE; if delimiter The contained value is not found in string and a negative limit is used, an empty array is returned, otherwise an array containing a single element of string is returned.
Example:
<?php header('content-type:text/html;charset=utf-8'); $str = 'PHP中文网,PHP教程,https://www.php.cn/,explode()函数,字符串转数组'; echo '<pre class="brush:php;toolbar:false">'; $arr = explode(',', $str); print_r($arr); $arr = explode(',', $str, 3); print_r($arr); $arr = explode(',', $str, -2); print_r($arr); $arr = explode(',', $str, 0); print_r($arr); $arr = explode('', $str); var_dump($arr); $arr = explode('@', $str, -1); print_r($arr); ?>
Output:
More programming related knowledge, Please visit: Programming Learning Course! !
The above is the detailed content of How to convert string to array in php?. For more information, please follow other related articles on the PHP Chinese website!