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

How to convert string to array in php

PHPz
PHPzOriginal
2023-04-20 13:51:06546browse

In PHP, we usually use strings as variables to store and process a set of data, but sometimes we need to convert strings into arrays to make data processing more convenient. This article will explain how to convert a string to an array using functions in PHP.

1. Use the explode function to split a string into an array

PHP provides a very convenient function called explode(), which can cut a string into arrays separated by specified delimiters. Multiple array elements.

Syntax:

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

Parameter description:

  • $delimiter: required, specifies where to split the string.
  • $string: required, the string to be split.
  • $limit: Optional, specifies the maximum number of array elements returned.

Sample code:

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

In the above code, we use commas as delimiters to split the string into an array, and the output result is:

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

2. Use the str_split function to split the string into an array of characters

The str_split() function can split a string into an array of each character.

Syntax:

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

Parameter description:

  • $string: required, the string to be split.
  • $split_length: Optional, the length of each array element.

Sample code:

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

In the above code, we split the string into a character array, and the output result is:

Array
(
    [0] => H
    [1] => e
    [2] => l
    [3] => l
    [4] => o
    [5] =>
    [6] => W
    [7] => o
    [8] => r
    [9] => l
    [10] => d
)

3. Use preg_split The function splits a string into an array

preg_split() function can split a string according to a regular expression.

Syntax:

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

Parameter description:

  • $pattern: required, specifies the segmentation rules.
  • $subject: required, the string to be split.
  • $limit: Optional, specifies the maximum number of array elements returned.
  • $flags: Optional, specifies how to split the string. Refer to the preg_split() function description in regular expressions.

Sample code:

$str = "apple1banana2orange3";
$arr = preg_split("/\d/", $str);
print_r($arr);

In the above code, we use regular expressions to remove numbers from the string, and the output result is:

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

Summary

PHP provides a variety of functions for converting strings to arrays. Different functions are suitable for different scenarios. Using these functions allows us to process data more conveniently. In actual use, we need to choose which function to use based on the actual situation.

The above is the detailed content of How 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