AppDividend
Latest Code Tutorials

PHP explode: How to Split String into Array Elements in PHP

The explode() function in PHP allows us to break the string into smaller text, with each break occurring at the same symbol.

PHP explode

PHP explode() is a built-in function that splits the string by the specified string into array elements. The explode() function takes three arguments and returns an array containing string elements. The explode() function breaks the string into an array. The explode() function is binary-safe.

PHP string to array

To convert string to array in PHP, use the explode() function. The explode() function breaks the string into an array, but the implode function returns a string from the elements of an array.

Syntax

See the syntax.

explode(separator,string,limit)

Arguments

The separator parameter is required to specify 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.

Implementation of explode() function

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

Passing limit parameter

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

Passing Multiple delimiters

You can pass the multiple delimiters to the 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

How to split empty string in PHP

To split the empty string in PHP, use the explode() function.

<?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, 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()

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. Any data preceding that limit is irrelevant since the parts are determined when a limit is found.

It should be said that when an empty delimiter is passed to explode, the function returns false and emits a warning. See the following code.

<?php

// app.php

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

See the output.

empty delimiter in explode() function

PHP explode vs. split

The main difference between the split() and explode() functions is splitting a large string. Both the functions are used to split a string. However, the split() function is used to split a string using a regular expression, while the explode() function is used to split a string using another string.

One more point, the split() function has been DEPRECATED as of PHP 5.3.0. Therefore, relying on this feature is discouraged; instead, use the explode() function.

The preg_split() is faster and uses PCRE regular expressions for regex splits.

Conclusion

To break the string and create the array, use the explode() function. That is it for splitting a string into an array the PHP.

Leave A Reply

Your email address will not be published.

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