Home  >  Article  >  Backend Development  >  How to convert php string to array

How to convert php string to array

PHPz
PHPzOriginal
2023-04-20 09:08:262124browse

PHP is a widely used programming language with the ability to handle strings. Strings are one of the most common data types, and converting strings to arrays is a very basic way of working with strings in PHP. In this article, the author will show you how to convert a string into an array.

1. The explode() function

Theexplode() function is one of the most commonly used methods for converting string arrays in PHP. Its syntax is:

array explode(string $delimiter, string $string, int $limit = PHP_INT_MAX)

where $delimiter is the delimiter, $string is the string to be converted, and $limit can be an optional limit to the number of elements returned in the array.

Look at the example below:

$str = "apple,banana,cherry";
$arr = explode(",", $str);

print_r($arr);

In the above example, we first define a string containing multiple comma delimiters and then convert it into an array using the explode() function . Finally, we print the array using the print_r() function.

2. str_split() function

The str_split() function is another way to convert a string into an array. It splits the string into individual characters and returns an array. Its syntax is:

array str_split(string $string, int $split_len = 1)

where $string is the string to be converted, $split_len is optional, indicating the number of characters that each array element should contain. For example:

$str = "Hello World";
$arr = str_split($str, 2);
print_r($arr);

In the above example, we first define a string and then split it into characters of length 2 using str_split() function. Finally, we print the array using the print_r() function.

3. preg_split() function

The preg_split() function can use regular expressions to convert a string into an array. Its syntax is similar to the explode() function:

array preg_split(string $pattern, string $string, int $limit = -1, int $flags = 0)

where $pattern is the regular expression, $string is the string to be converted, and $limit is an optional limit for the number of elements returned in the array. parameter.

For example, suppose we have a string with key-value pairs separated by the "&" symbol:

$str = "name=john&age=25&gender=male";
$arr = preg_split("/&/", $str);

print_r($arr);

In the above example, we first define a string and then use The preg_split() function splits it into characters of length 2. Finally, we print the array using the print_r() function.

Summary

The above methods can convert strings into arrays, and you can choose one of them according to your needs. The pros and cons of each method depend on your specific application scenario. You should choose the method that best suits you based on the specific situation.

The above is the detailed content of How to convert php string to array. 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