Syntax of explode() function and Usage.
explode(separator,string,limit)
Parameter Values
separator -> Specifies where to break the string.
string -> The input string to split in array.
limit -> This is an optional.Specifies the number of array elements to return.
1.if limit parameter is positive(greater than 0)
Return an array with a maximum of limit elements.
2.if limit parameter is negative(less than 0)
Return an array except for the last -limit.
3.if limit parameter is zero(0)
Return an array with one element
Let's learn
1.Create string
$string = "Hello world! Welcome to BAJARANGISOFT.";
2.Print string using explode() function.
print_r(explode(" ", $string));
Example(1)
<?php
$string = "Hello world! Welcome to BAJARANGISOFT.";
print_r (explode(" ",$string));
?>
Example(2)
<?php
$string = " Cherries,Mango,Grape,Apple.";
print_r(explode(',',$string,0));// zero limit
echo "<br>";
print_r(explode(',',$string,3)); // positive limit
echo "<br>";
print_r(explode(',',$string,-1)); // negative limit
?>
Complete Code of explode() Function:
<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP explode function</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="text-center">
<h1>PHP explode() Function</h1>
</div>
<br>
<h2>The explode() function used to split a string in different strings</h2>
<div class="well">
<?php
$string_1 = "Hello world!. Welcome to BAJARANGISOFT.";
print_r(explode(" ", $string_1));
echo "<br>";
$string_2 = " Cherries,Mango,Grape,Apple.";
print_r(explode(',', $string_2, 0));// zero limit
echo "<br>";
print_r(explode(',', $string_2, 3)); // positive limit
echo "<br>";
print_r(explode(',', $string_2, -1)); // negative limit
?>
</div>
<br>
</div>
</body>
</html>