Chose Language:
Author: Admin/Publisher |finished | checked

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 / Sintax
explode($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.

Examples

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"
}
Category: en-php
Something wrong? If you found an error or mistake in the content you can contact me on Twitter | @luisg2249_luis.
Last 4 post in same category