Home Daily Programming PHP Knowledge How to split string into array in PHP

How to split string into array in PHP

Dec 07, 2018 pm 04:38 PM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)