Before explaining the explode function, you first need to have knowledge of arrays and strings, then you will be able to understand completely. This is a function of PHP which depends on the string and array. With the help of the explode function, you can convert a string to an array. In the previous tutorials, we have had to discuss the Array and string. If you do not have knowledge of arrays and strings, then you will not be able to understand the explode() function easily. First of all, you need to know about Array and String.
References -
String in PHP.
Arrays in PHP
Definition & Use
Explode function is used to split the string into the array: In another way, you can say that the explode function helps to convert a string into an array.
Syntax of the explode function
explode(string delimiter,string, limit)
Parameter -
Delimiter-
The boundary string.
String-
The string name (string variable)
Limit- Limit may positive or negative..
Examples of explode() function
In this example, we create string and split into an array using explode() function, it splits it into the array.
Example
<?php
$line="My name is Vishal.";
$line_arr= explode(" ",$line);
print_r($line_arr);
?>
Run
In this above example, we use string and store it in a variable. After storing the string in the variable, that variable is passed in the second parameter of the explode function. After that, we have stored this process in a different variable, with the help of the print_r() function, we display the standard array formate.
$line -- A variable.
My name is Vishal--String
explode(" ",$line); -- explode() function with second parameter.
Now pass two parameters in a new example.
Example
<?php
$line="My name is Vishal";
$line_arr= explode("n",$line);
print_r($line_arr);
?>
Run
According to the explode function, we can pass parameters. In this above example, we have passed two parameters.
1. String delimiter.
2. String.
explode("n",$line);---
n is a first parameter (String delimiter)
$line second parameter of explode() function.(String variable)
Example
<?php
$products = "address1 address2 address3 address4 address5";
$quantity= explode(" ", $products);
echo $quantity[0]."
";
echo $quantity[1]."
";
echo $quantity[2]."
";
echo $quantity[3]."
";
?>
Output-
address1
address2
address3
address4
The positive limit in explode function
If the limit is positive. A word that doesn't contain the delimiter will simply
return a one-length array of the original string.
In a simple way, Suppose that if the limit is positive 2, then the complete string divides into two parts of array index .0 index number and 1 index number. If the limit is 1 then the string would split into an index number 0.
Limit positive 2
<?php
$products= 'Mobile|Tablet|Laptop|LED';
// positive limit
print_r(explode('|', $products, 2));
?>
Output
Array ( [0] => Mobile [1] => Tablet|Laptop|LED )
if the limit is positive 1
<?php
$products= 'Mobile|Tablet|Laptop|LED';
// positive limit
print_r(explode('|', $products, 1));
?>
Output
Array ( [0] => Mobile|Tablet|Laptop|LED )
The Negative limit in explode function
If the limit is negative, then the Array's last item gets removed. For example, if the limit is - 1 then the last one item gets deleted from the array. The array does not include the last item. If the explode function has a limit of -2 then the last 2 items do not include in the array. You will get a better clarification with examples. Let's do it from Limit -1 to -4
If the limit is -1
<?php
$products= 'Mobile|Tablet|Laptop|LED';
// negative limit -1
print_r(explode('|', $products, -1));
?>
Output-
Array ( [0] => Mobile [1] => Tablet [2] => Laptop )
If the limit is -2
<?php
$products= 'Mobile|Tablet|Laptop|LED';
// negative limit -2
print_r(explode('|', $products, -2));
?>
Output-
Array ( [0] => Mobile [1] => Tablet )
If the limit is -3
<?php
$products= 'Mobile|Tablet|Laptop|LED';
// negeative limit -3
print_r(explode('|', $products, -3));
?>
Output-
Array ( [0] => Mobile )
If the limit is -4
<?php
$products= 'Mobile|Tablet|Laptop|LED';
// negeative limit -3
print_r(explode('|', $products, -4));
?>
Output-
Array ( )
Recommended Posts:-