AppDividend
Latest Code Tutorials

PHP Explode Function Example | PHP String Explode Tutorial

0

PHP Explode Function Example | PHP String Explode Tutorial is today’s topic. PHP explode() function is used to “Split a string by the specified string into parts, i.e., it breaks the string into the array.” The explode function in PHP allows us to break the string into smaller text with each break occurring at the same symbol. This symbol is known as the delimiter. Using the explode function, we will create an array from the string. PHP explode() function breaks the string into an array, but the implode function returns a string from the elements of an array.

PHP Explode Function Example

The explode() function breaks the string into an array. The explode() function is binary-safe. See the syntax.
explode(separator,string,limit)

The separator parameter is required, and it specifies where we break the string.

The string parameter is required. It is the string to split.

The limit parameter is optional, and it specifies the number of array elements to return.

See the following example.

<?php

// app.php

$brandsA = 'LouisVuitton Hermès Gucci Prada Chanel Cartier';
$arrA = explode(" ", $brandsA);
print_r($arrA);

See the below output.

PHP Explode Function Example

 

The limit parameter example

See the following code where we pass the third parameter limit and see the output.

<?php

// app.php

$strA = 'ronaldo|messi|neymar|bale';
$arrA = explode("|", $strA, 2);
print_r($arrA);

See the output.

The limit parameter example

 

Use the negative limit and see the result.

<?php

// app.php

$strA = 'ronaldo|messi|neymar|bale';
$arrA = explode("|", $strA, -1);
print_r($arrA);

See the below output.

Use the negative limit

 

Multiple delimiters in PHP Explode Function

See the following code.

<?php

// app.php

function multipleexplode ($delimiters,$string) {
  $phase = str_replace($delimiters, $delimiters[0], $string);
  $processed = explode($delimiters[0], $phase);
  return  $processed;
}

$longStr = "appdividend is cool: krunal is author, and this will be exploded. this also | this one too :)";
$op = multipleexplode(array(",",".","|",":"),$longStr);
print_r($op);

See the output.

Multiple delimiters in PHP Explode Function

 

Split an empty String

Let’s see the scenario where we try to split the empty string.

<?php

// app.php

$strA = "";
$result = explode(",", $strA);
print_r($result);

See the following example.

Split an empty String

 

If you split the empty string, you get back the one-element array with 0 as the key and the empty string for the value. If we want to solve this then, use the array_filter() without callback. Quoting manual page “If the callback function is not supplied, the array_filter() function will remove all the entries of input that are equal to FALSE..”

Trim whitespaces using explode() method

We can use the explode() function to trim the white space from the string.

With the help of the array_map() function and explode() function, we can trim the white spaces and split the string into an array. See the following code.

<?php

// app.php

$str = "one  ,two  ,       three  ,  four    "; 
print_r(array_map('trim',explode(",",$str)));

See the output.

Trim whitespaces using explode() method

 

Explode does not parse the string by delimiters, in the sense that we expect to find tokens between the starting and ending delimiter, but instead splits the string into pieces by using the string as the boundary of each part. Once that boundary is discovered the string is split. Whether or not that limit is proceeded or superseded by any data is irrelevant since the parts are determined at the point a limit is discovered.

It should be said that when an empty delimiter is passed to explode, the function not only will return false but will also emit a warning. See the following code.

<?php

// app.php

$data = 'aqswwdwdwdwdwdw';
print_r(explode('', $data));

See the output.

empty delimiter in explode() function

 

Finally, PHP Explode Function Example | PHP String Explode Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.