The PHP string, Explode() function is an interesting thing. Now I am going to explain the explode() function. I don't know how much I can, but I'm trying to make it understandable to you.
Meaning of explode(): The main function of explode () is to divide a string into smaller parts and store them in an array. In this case, we should indicate by a separator where the string will break, including a limit(limit is optional).
Let's think about chopping a cucumber. Suppose, cucumber is our string,
the knife is our separator(delimiter), and the pieces of cucumber are array elements. The function of explode() is like chopping a cucumber.
Meaning of explode(): The main function of explode () is to divide a string into smaller parts and store them in an array. In this case, we should indicate by a separator where the string will break, including a limit(limit is optional).
Let's think about chopping a cucumber. Suppose, cucumber is our string,
the knife is our separator(delimiter), and the pieces of cucumber are array elements. The function of explode() is like chopping a cucumber.
Function signature:
explode('delimiter',string,limit);
Delimiter: This is a separator (it can be any kind of symbol). Actually, the separator is called a delimiter. When the explode function finds a symbol in a string, it returns an array of the string at that place.
String: The string to be split by the explode() function.
Limit: This is an optional way. I am going to describe details about the limit parameter below:
Zero Limit:
while limit=0, the value of the explode () will return in an array element. We can make it clear with an example:
<? php
$string= 'we are students of PHP.'
echo(explode(' ',$string,0));
?>
output will be, Array[0]=we are the student of php.
Note: (' ') this single quotation with a white space is called a delimiter. If the delimiter contains an empty separator, then the result will be false or wrong.
positive limit:
It returns an array with the maximum limit of the element.
when limit=2, it will take two places in an array. Let's make it clear with an example,
<? php
$string= 'we, are, the, student, of, php.'
echo(explode(', ',$string,2));
?>
output will be, Array[0]=we
Array[1]=are, the, student, of, php.
Negative Limit:
when limit = -1, the explode function will return except the last one. With an example, it will be clear,
<? php
$string='we/ are/ the/ student/ of/ php.'
echo(explode('/',$string,-1));
?>
output will be, Array[0]=we
Array[1]=are
Array[2]=the
Array[3]=student
Array[4]= of

Comments
Post a Comment