PHP explode
PHP explode is practically the opposite of implode. This divides a string in substrings that are stored in an array
This function receives 3 parameters, the delimiter (string) that is where the string will be cut, another one that is the string and a third parameter that is optional called limit.
Explode Syntaxis
Sintaxis / Sintaxexplode($delimiter,$string,$limit)
$delimiter
– Is a string that contains the character or string of character where the string will be cut.
$string
– the string where explode will be implemented.
$limit
– if it’s used, explode will return the quantity of elements specified in $limit in the array and the last one is the rest of the string. In case that $limit have negative value will return the array without quantity of elements specified in $limit.
Use Cases of PHP Explode
In this section, we will see some situations in which we might use the explode
function. Remember that explode
separates a string into an array based on a specified character.
CSV
Sometimes you might need to work with CSV files. CSV files have a structure like the following:
Lunes,martes,miércoles,jueves,viernes,sábado,domingo
The above data would correspond to a single row.
Most likely, we will need to have the data separated, and that’s where explode
comes in.
$linea = " Lunes,martes,miércoles,jueves,viernes,sábado,domingo"; $datos = explode(",", $linea);
URLs
We have done this many times on the blog, but obviously, we might want to separate the URL in many cases, for example in routes for a website, a framework we are building, extracting data from the URL, etc
There is an example on examples.
URL Parameters
$parametros = "param1=value1¶m2=value2¶m3=value3"; $parametrosArray = explode("&", $parametros);
Dates
Let’s suppose we are doing something similar to WordPress. It has an update folder, and within the update folder, it creates folders for the year and month when the files were uploaded. We could use explode
to create these folders
<?php $fechahoy= time(); $fh= date('Y-m-d', $fechahoy); $arrfecha = explode("-",$fh); foreach ($arrfecha as $element){ echo "<p>$element</p>"; }
These are some examples where you could use explode
.
Examples of PHP explode function
Imagine that we have a URL, and we want to get a value from it.
with variable $limit not specified<?php $url = "hombres/camisetas/rojas"; $array = explode("/",$url); echo $array[0]."\n"; echo $array[1]."\n"; echo $array[2]."\n"; ?>
hombres camisetas rojas
Using $limit
$limit=2<?php $url = "hombres/camisetas/rojas"; $array = explode("/",$url,2); var_dump($array); ?>
array(2) { [0]=> string(7) "hombres" [1]=> string(15) "camisetas/rojas" }
Let’s change $limit= -2
explode example with $limit=-2<?php $url = "hombres/camisetas/rojas"; $array = explode("/",$url,-2); var_dump($array); ?>
array(1) { [0]=> string(7) "hombres" }
In last example we will change $limit=-1 to understand what does $limit
Example with $limit=-1<?php $url = "hombres/camisetas/rojas"; $array = explode("/",$url,-1); var_dump($array); ?>
array(2) { [0]=> string(7) "hombres" [1]=> string(9) "camisetas" }