php explode

In php explode() has the following prototype:

array explode(string separator, string input [, int limit]);

This function takes a string input and splits it into pieces on a specified separator string. The pieces are returned in an array. You can limit the number of pieces with the optional limit parameter

To get the domain name from the customer’s email address in the script, you can use the following code:

$email_array = explode(‘@’, $email);

This call to explode() splits the customer’s email address into two parts: the username, which is stored in $email_array[0], and the domain name, which is stored in $email_array[1]

Other Example of explode()

// Example 1
$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2

// Example 2
$data = "foo:*:1023:1000::/home/foo:/bin/sh";
list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
echo $user; // foo
echo $pass; // *


More Example of explode in php

   A string that doesn't contain the delimiter will simply
   return a one-length array of the original string.
$input1 = "hello";
$input2 = "hello,there";
$input3 = ',';
var_dump( explode( ',', $input1 ) );
var_dump( explode( ',', $input2 ) );
var_dump( explode( ',', $input3 ) );

$str = 'one|two|three|four';

// positive limit
print_r(explode('|', $str, 2));

// negative limit
print_r(explode('|', $str, -1));

Leave a Comment