Home >Backend Development >PHP Problem >PHP converts the queried characters into an array

PHP converts the queried characters into an array

WBOY
WBOYOriginal
2023-05-05 20:40:05672browse

In PHP development, it is often necessary to convert the queried string into an array for subsequent operations. This article will introduce how to convert the query string into an array in PHP.

1. Use the explode function

The explode function in PHP can split a string into an array according to the specified delimiter. We can use this function to split the queried string into arrays according to specific delimiters.

Sample code:

$str = 'apple,banana,orange';
$arr = explode(',', $str);
print_r($arr);

Output result:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
)

In the above example, we first define a string $str, whose values ​​are separated by commas. Then use the explode function to separate the string into an array $arr based on commas, and finally use the print_r function to output the contents of the array.

2. Use the str_split function

The str_split function in PHP can split a string into a character array of specified length. We can use this function to split the queried string into arrays according to the specified length.

Sample code:

$str = 'hello';
$arr = str_split($str);
print_r($arr);

Output result:

Array
(
    [0] => h
    [1] => e
    [2] => l
    [3] => l
    [4] => o
)

In the above example, we use the str_split function to split a string $str into An array $arr, and finally use the print_r function to output the contents of the array.

3. Use the preg_split function

The preg_split function in PHP can use regular expressions to split strings. We can use this function to split the queried string into arrays according to a specific regular expression.

Sample code:

$str = 'apple1orange2banana3pear';
$arr = preg_split('/\d/', $str);
print_r($arr);

Output result:

Array
(
    [0] => apple
    [1] => orange
    [2] => banana
    [3] => pear
)

In the above example, we first define a string $str, whose value contains numbers and fruit names . Then use the preg_split function to split the string into an array $arr based on numbers, and finally use the print_r function to output the contents of the array.

Summary:

In PHP, there are many ways to convert the queried string into an array. The above introduces three commonly used methods, namely using the explode function, str_split function and preg_split function. When using it, you need to consider the characteristics and needs of the string and choose the most appropriate method for operation.

The above is the detailed content of PHP converts the queried characters into an 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