How to split string into array in PHP

藏色散人
Release: 2018-12-12 09:29:37
Original
19614 people have browsed it

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.

How to split string into array 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 = &#39;PHP 中文网 是一个 学习 PHP 的好地方&#39;;
echo "<pre class="brush:php;toolbar:false">";
print_r(explode(" ",$my_str));
Copy after login

is split into an array and the result is as follows:

How to split string into array in PHP

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 = &#39;PHP 中文网 是一个 学习 PHP 的好地方&#39;;
echo "<pre class="brush:php;toolbar:false">";
print_r(explode(" ",$my_str,4));
Copy after login

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:

How to split string into array in PHP

#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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template