Split a string into an array using PHP's explode() function

PHPz
Release: 2023-11-03 08:16:01
Original
1176 people have browsed it

Split a string into an array using PHPs explode() function

Use PHP's explode() function to split a string into an array. Specific code examples are required

In PHP development, string splitting is a very common operation. It can be implemented using original string manipulation functions, or it can be implemented using PHP's built-in explode() function. The explode() function splits the string into an array according to the specified delimiter, which is very convenient to use. This article will introduce in detail how to use the explode() function to split a string into an array, and provide specific code examples.

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

The syntax of the explode() function is as follows:

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

Parameter description:
$delimiter: Required, specifies the character or string used to separate strings.
$string: required, the string to be split.
$limit (optional): Specify the limit on the number of array elements divided into.

For example, to split a space-separated string into an array, you can use the following code:

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

The output result is as follows:

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

Split a comma-separated character To split a string into an array, you can use the following code:

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

The output result is as follows:

Array ( [0] => apple [1] => orange [2] => banana )
Copy after login
Copy after login
  1. Use the explode() function and loop to split multiple comma-separated strings into embedded Set of arrays

Sometimes, you need to split multiple comma-separated strings into nested arrays, which can be achieved by combining loops and the explode() function. For example, to split the following two strings into nested arrays:

$str1 = "apple,orange"; $str2 = "banana,pear";
Copy after login

You can use the following code:

$strArr = array($str1, $str2); $array = array(); foreach($strArr as $str) { $arr = explode(",", $str); $array[] = $arr; } print_r($array);
Copy after login

The output results are as follows:

Array ( [0] => Array ( [0] => apple [1] => orange ) [1] => Array ( [0] => banana [1] => pear ) )
Copy after login

Through the above code examples, I believe You've learned how to split a string into an array using the explode() function. In PHP development, string processing is a very important link, and mastering string operation functions is an essential skill.

The above is the detailed content of Split a string into an array using PHP's explode() function. 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!