Code has been added to clipboard!

PHP explode()

Reading time 1 min
Published Aug 14, 2017
Updated Oct 10, 2019

PHP explode: Main Tips

  • PHP explode function is used to get an array of strings from a single string.
  • Using explode() in PHP is completely binary-safe.
  • It was introduced in PHP4 with the limit parameter, though negative limits weren't allowed until PHP 5.1.
Udacity
Pros
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid certificates of completion
Coursera
Pros
  • Professional service
  • Flexible timetables
  • A variety of features to choose from
Main Features
  • Professional certificates of completion
  • University-level courses
  • Multiple Online degree programs
Datacamp
Pros
  • Great user experience
  • Offers quality content
  • Very transparent with their pricing
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable

Syntax: Parameters Explained

The correct PHP explode function syntax is displayed below:

explode(separator,string,limit);

Let's figure out what each parameter represents when using PHP explode function:

Parameter Description
separator Needed. Defines the place where should PHP explode string. Cannot be an empty string!
string Needed. Specifies the exact string to split.
limit Optional. Defines the number of elements in an array to return after:
> 0 - Returns the array including a max of limit element(s)
< 0 - Returns the array except for the last -limit elements()
0 - Returns an array with one element

Usage: Code Examples

Look at the example below. It shows different outcomes that can be achieved using limit parameter with PHP explode function:

Example
<?php
  $string = 'first,second,third,fourth';

  // limit - zero
  print_r(explode(',', $string, 0));

  // limit - positive
  print_r(explode(',', $string, 2));

  // limit  - negative
  print_r(explode(',', $string, -1));
?>

In this next example, comma is used as a separator. See how that turns out:

Example
<?php
  $string = "This is example, with, commas!";
  print_r (explode(",", $string));
?>

Latest Udacity Coupon Found:

Verified STAFF PICK

75% OFF COURSES

Udacity Black Friday Offer

The best time to save on Udacity courses is now - follow this coupon to access a 75% Udacity Black Friday discount & enjoy learning at a very low cost!

Expiration date: 28/01/2021
3176 People Used
Only 97 Left
Rating
5.0