Home > Backend Development > PHP Problem > php convert string to array

php convert string to array

WBOY
Release: 2023-05-06 09:20:07
Original
574 people have browsed it

PHP, as a scripting language, provides a wealth of string processing functions. Converting string to array is also one of the common functions of PHP. Here we will introduce in detail how to use PHP to convert a string into an array.

First introduce the string splitting function explode in PHP. This function can split a string into an array based on the specified delimiter.

The syntax is as follows:

array explode ( string $delimiter , string $string [, int $limit = PHP_INT_MAX ] )
Copy after login

Among them, delimiter represents the delimiter, string represents the string that needs to be split, and limit represents the limit of the array length, which is an optional parameter. This function returns an array.

The following is a simple example showing how to use the explode function to convert a string into an array:

// 定义一个字符串
$str = "PHP,Java,Python,C++";
// 使用explode函数将字符串转化成数组
$arr = explode(",", $str);
// 遍历数组并输出
foreach ($arr as $value) {
    echo $value . "<br>";
}
Copy after login

The output result is as follows:

PHP
Java
Python
C++
Copy after login

In the above code, we convert the string Converts to an array of four elements, each of which is a programming language. Using a foreach loop to iterate over an array, you can print each element in the array one by one.

In addition to using the explode function, PHP also provides some other practical string processing functions, which can also convert strings into arrays. Here are two commonly used functions, namely str_split and preg_split.

1. str_split function

str_split is a function used to split a string into a character array. The so-called character array is an array composed of single characters in a string. The following is a sample code that uses the str_split function to split a string into a character array:

// 定义一个字符串
$str = "Hello World!";
// 使用str_split函数将字符串转化成字符数组
$arr = str_split($str);
// 遍历数组并输出
foreach ($arr as $value) {
    echo $value . "<br>";
}
Copy after login

The output results are as follows:

H
e
l
l
o

W
o
r
l
d
!
Copy after login

As you can see, after using the str_split function to convert the string into a character array, Each element in the array is a separate character.

2. preg_split function

The preg_split function is a string splitting function based on regular expressions. It can split a string into arrays based on regular expressions. The following is a sample code that uses the preg_split function to split a string into an array:

// 定义一个字符串
$str = "apple orange banana";
// 使用preg_split函数将字符串转化成数组
$arr = preg_split("/\s+/", $str);
// 遍历数组并输出
foreach ($arr as $value) {
    echo $value . "<br>";
}
Copy after login

The output results are as follows:

apple
orange
banana
Copy after login

As you can see, after using the preg_split function to split the string into an array, the Each element of is a space-separated string.

So far, we have introduced three methods of PHP processing strings and converting them into arrays. Please choose the appropriate method according to actual needs to better complete the string processing work.

The above is the detailed content of php convert string to array. For more information, please follow other related articles on the PHP Chinese website!

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