In the daily project development process, long strings may need to be split into arrays in order to be displayed or used for judgment and verification. Then it is easy to split the string into an array. We can split it directly through the explode function in PHP.
Let’s use a simple example to introduce to you the method of splitting a PHP string into an array.
The code example is as follows:
<?php $my_str = 'PHP 中文网 是一个 学习 PHP 的好地方'; echo "<pre class="brush:php;toolbar:false">"; print_r(explode(" ",$my_str));
is split into an array and the result is as follows:
explode function returns the characters An array composed of strings. Each element is a substring of the string string. They are divided by the first parameter as the boundary point. In the above code, the first parameter uses the " " space as the dividing point, and the second parameter The parameter represents the string to be split.
And explode can also set the third parameter:
<?php $my_str = 'PHP 中文网 是一个 学习 PHP 的好地方'; echo "<pre class="brush:php;toolbar:false">"; print_r(explode(" ",$my_str,4));
Here we set the third parameter 4, which means that the returned array contains up to 4 elements, and the last element will contain string the remaining part.
The splitting result is as shown below:
#Note: If the third parameter is a negative number, all elements except the last "negative number" elements will be returned. element. If the third argument is 0, it will be treated as 1.
This article is a detailed introduction to PHP splitting strings into arrays. It is very simple and easy to understand. I hope it will be helpful to friends in need!
The above is the detailed content of How to split string into array in PHP. For more information, please follow other related articles on the PHP Chinese website!