How to Split a String into an Array with the PHP explode() Function

09/04/2021

Contents

In this article, you will learn how to split a string into an array with the PHP explode() function.

PHP explode() Function

The explode() function splits a string into an array.

Syntax:

explode($separator,$string,[$limit])

$separator: Specifies the non-empty separator to split the string.

$string: The string to split.

$limit: Specifies the maximum number of array elements to return.

If the string does not contain any of the specified separators, the string is returned unchanged.
If $limit is specified, the last array element will contain all remaining strings.

Sample

Sample Code

<?php
  $str = "Plantpot is a web development blog.";
  print_r (explode(" ",$str,3));
?>

Result

Array ( 
  [0] => Plantpot 
  [1] => is 
  [2] => a web development blog. 
)