Sitemap / Advertise

Information



Tags



Share

What is the explode function in PHP?

Advertisement:


read_later

Read Later

Keywords



Keywords



read_later

Read Later

Information

Tags

Share





Advertisement

Advertisement




Definition

The split function in PHP is deprecated by the explode function.

When you want to split a string by a string in PHP, you should use the explode function. It separates a string into an array of strings by splitting them according to the delimiter string which is mostly known as separator. In other words, it returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter.(1)

Parameters(1):

Parameter Description
delimiter The boundary string.
string The input string.
limit If limit is set and positive, the returned array will contain a maximum of limit elements with the last element containing the rest of string. If the limit parameter is negative, all components except the last -limit are returned. If the limit parameter is zero, then this is treated as 1.

Code

In this example, I used the explode function to split a string named content two times with two different separators. I did not use limit parameters but you can add it to the code if you want.


<?php
$content = "Hello-my-name-is,John-Doe-and,-I-am-working-on,PHP";

$splitInComma = explode(",", $content);

$splitInDash = explode("-", $splitInComma[1]);

$firstName = $splitInDash[0];

echo $firstName;

?>


Result:

John

References

(1) http://php.net/manual/en/function.explode.php