search
HomeDaily ProgrammingPHP KnowledgeHow to split string into array in PHP

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));

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));

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

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft