Home >Backend Development >PHP Tutorial >The meaning of explode function in php
PHP explode() function splits the string into an array according to the specified delimiter (or regular expression), and the return value is an array. Usage includes: Use commas to separate strings: explode(",", "apple,banana,cherry") Use regular expressions to separate strings: explode("-", "123-456-7890") Limit the number of returned array elements Number: explode(",", "apple,banana,cherry", 2) will only return ["apple", "banana"]
PHP explode() function
explode() function is used to split a string into arrays according to specified characters or regular expressions.
Syntax:
<code class="php">array explode(string delimiter, string string, int limit)</code>
Parameters:
Return value:
An array containing the split string.
Usage:
explode() function splits the string into an array according to the specified delimiter. For example:
<code class="php">$str = "Hello, world!"; $arr = explode(",", $str); print_r($arr);</code>
Output:
<code>Array ( [0] => Hello [1] => world! )</code>
Example:
<code class="php">$str = "apple,banana,cherry"; $arr = explode(",", $str);</code>
<code class="php">$str = "123-456-7890"; $arr = explode("-", $str);</code>
<code class="php">$str = "apple,banana,cherry"; $arr = explode(",", $str, 2);</code>
This will only return an array of two elements, i.e. ["apple", "banana"].
The above is the detailed content of The meaning of explode function in php. For more information, please follow other related articles on the PHP Chinese website!