Home >Backend Development >PHP Tutorial >Split string into array using PHP function 'explode'

Split string into array using PHP function 'explode'

WBOY
WBOYOriginal
2023-07-24 23:09:051526browse

Use the PHP function "explode" to split a string into an array

In PHP development, you often encounter situations where you need to split a string according to the specified delimiter. At this time, we can use PHP's built-in function "explode" to convert string to array. This article will introduce how to use the "explode" function to split a string and give relevant code examples.

The basic syntax of the "explode" function is as follows:

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

Among them, the parameter "delimiter" is the specified delimiter, and the parameter "string" is the string to be split. The optional parameter "limit" is used to limit the number of array elements returned.

The following is an example of using the "explode" function to split a string into an array:

<?php
$str = "Apple,Banana,Orange,Strawberry";
$delimiter = ",";
$fruits = explode($delimiter, $str);

print_r($fruits);
?>

Run the above code, the output is as follows:

Array
(
    [0] => Apple
    [1] => Banana
    [2] => Orange
    [3] => Strawberry
)

In the above example , first defines a string "$str" containing multiple fruit names. Then, using commas as delimiters, the "explode" function was called to split the string into an array. Finally, use the "print_r" function to print out the split array elements.

As you can see, the split array contains the name of each fruit as an array element.

In addition to using a single character as the delimiter, you can also use multiple characters as the delimiter, for example:

<?php
$str = "Hello World! How are you today?";
$delimiter = " ";
$words = explode($delimiter, $str);

print_r($words);
?>

Run the above code, the output result is as follows:

Array
(
    [0] => Hello
    [1] => World!
    [2] => How
    [3] => are
    [4] => you
    [5] => today?
)

Above In the example, the string is split into an array using spaces as delimiters, and the result is that each word is an array element.

It should be noted that if you want to split a string into an array according to each character, you can use the "str_split" function instead of the "explode" function. For example:

<?php
$str = "Hello";
$characters = str_split($str);

print_r($characters);
?>

Run the above code, the output result is as follows:

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

In this article, we introduced how to use the PHP function "explode" to split a string into an array, and gave Related code examples. I hope this article helps you understand and use the "explode" function.

The above is the detailed content of Split string into array using PHP function 'explode'. 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