Home  >  Article  >  Backend Development  >  Various methods to convert string to array in PHP

Various methods to convert string to array in PHP

PHPz
PHPzOriginal
2023-04-25 10:32:41528browse

In PHP development, sometimes multiple strings need to be converted into arrays for processing. This article will introduce various methods of converting strings into arrays in PHP.

Method 1:

(PHP 5, PHP 7)

string explode ( string $delimiter , string $string [, int $limit = PHP_INT_MAX ] )

This function splits the string into an array according to the specified delimiter $delimiter and returns it, where $limit is an optional parameter used to specify the maximum length of the split array.

Sample code:

$str1 = "apple,banana,orange";
$arr1 = explode(",", $str1); // 将$str1以“,”拆分成数组$arr1
var_dump($arr1);
// 输出:
// array(3) {
//   [0]=>
//   string(5) "apple"
//   [1]=>
//   string(6) "banana"
//   [2]=>
//   string(6) "orange"
// }

Method 2:

(PHP 5, PHP 7)

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

This function is the same as method one, the difference lies in the function name and parameter passing method.

Sample code:

$str2 = "apple:banana:orange";
$arr2 = explode(":", $str2);
var_dump($arr2);
// 输出:
// array(3) {
//   [0]=>
//   string(5) "apple"
//   [1]=>
//   string(6) "banana"
//   [2]=>
//   string(6) "orange"
// }

Method three:

(PHP 5 >= 5.3.0, PHP 7)

array str_split ( string $string [, int $split_length = 1 ] )

This function splits the string into an array of specified length and returns it, where $split_length is an optional parameter used to specify the length of the split.

Sample code:

$str3 = "Hello World";
$arr3 = str_split($str3);
var_dump($arr3);
// 输出:
// array(11) {
//   [0]=>
//   string(1) "H"
//   [1]=>
//   string(1) "e"
//   [2]=>
//   string(1) "l"
//   [3]=>
//   string(1) "l"
//   [4]=>
//   string(1) "o"
//   [5]=>
//   string(1) " "
//   [6]=>
//   string(1) "W"
//   [7]=>
//   string(1) "o"
//   [8]=>
//   string(1) "r"
//   [9]=>
//   string(1) "l"
//   [10]=>
//   string(1) "d"
// }

Method 4:

(PHP 5 >= 5.5.0, PHP 7)

array str_split ( string $string [, int $length = 1 [, string $encoding = "UTF-8" ]] )

This function is the same as method 3, the difference is that the character encoding can be specified.

Sample code:

$str4 = "你好,世界";
$arr4 = str_split($str4);
var_dump($arr4);
// 输出:
// array(7) {
//   [0]=>
//   string(3) "你"
//   [1]=>
//   string(3) "好"
//   [2]=>
//   string(3) ","
//   [3]=>
//   string(3) "世"
//   [4]=>
//   string(3) "界"
//   [5]=>
//   string(0) ""
//   [6]=>
//   string(0) ""
// }

Method five:

(PHP 7)

array str_split ( string $string [, int $length = 1 [, string $encoding = "UTF-8" ]] )

This function is the same as method 4, the difference is that it is only applicable to PHP7 and above.

Sample code:

$str5 = "Hello, world!";
$arr5 = preg_split('//u', $str5, -1, PREG_SPLIT_NO_EMPTY);
var_dump($arr5);
// 输出:
// array(14) {
//   [0]=>
//   string(1) "H"
//   [1]=>
//   string(1) "e"
//   [2]=>
//   string(1) "l"
//   [3]=>
//   string(2) "l"
//   [4]=>
//   string(1) "o"
//   [5]=>
//   string(1) ","
//   [6]=>
//   string(1) " "
//   [7]=>
//   string(1) "w"
//   [8]=>
//   string(1) "o"
//   [9]=>
//   string(1) "r"
//   [10]=>
//   string(1) "l"
//   [11]=>
//   string(1) "d"
//   [12]=>
//   string(1) "!"
//   [13]=>
//   string(0) ""
// }

The above are the various methods of converting strings into arrays in PHP. You can choose the method that suits you according to your actual needs.

The above is the detailed content of Various methods to convert string to 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