Home >Backend Development >PHP Problem >How to convert string to array in php?

How to convert string to array in php?

青灯夜游
青灯夜游Original
2020-11-05 14:57:074598browse

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)".

"How

#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(&#39;content-type:text/html;charset=utf-8&#39;);
$str = &#39;PHP中文网,PHP教程,//m.sbmmt.com/,explode()函数,字符串转数组&#39;;
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
$arr = explode(&#39;,&#39;, $str);
print_r($arr);
$arr = explode(&#39;,&#39;, $str, 3);
print_r($arr);
$arr = explode(&#39;,&#39;, $str, -2);
print_r($arr);
$arr = explode(&#39;,&#39;, $str, 0);
print_r($arr);
$arr = explode(&#39;&#39;, $str);
var_dump($arr);
$arr = explode(&#39;@&#39;, $str, -1);
print_r($arr);
?>

Output:

"How

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn