php explode split string into an array

0
328
php explode split string
php explode split string

php explode split string into an array.

Php explode() is an in-built function used to disintegrate a string into unrelated arrays.

In this post I practically carried out test on php explode examples, parameters with the php explode syntax

The explode() function disintegrate a string based on a string delimiter, that is, it divides the string wherever the delimiter character occurs.

Main usage of php Explode

This functions give back an array accommodating the strings formed by dividing the major string.

Structure:

explode(delimiter, string, limit)

Parameters of php explode

The php explode function takes up three parameters of which two are required and one is optional. The three parameters are explained down here:

Delimiter: This places the borderline string within the input string. And it is required.

String:The input string to be split in an array. It also required.

Limit: Used to refer the high number of elements in the output array. This parameter can be any integer ( positive , negative or zero). Only this one is optional.

Return Values

Returns an array of strings made by breaking the string parameter on the borderlines formed by the delimiter.

If delimiter is a vacant string (""), explode() will return FALSE. when delimiter has a value that is not accommodated in string and a negative limit is used, then a vacant array will be restored, or an array accommodating string will be restored.

Examples of php explode

#1 Example: explode() array

<?php
// 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; // *

?>

#2 Example : explode() a string

<?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 ) );

?>
array(1)
(
[0] => string(5) “hello”
)
array(2)
(
[0] => string(5) “hello”
[1] => string(5) “there”
)
array(2)
(
[0] => string(0) “”
[1] => string(0) “”
)




#3 Example : Explode() limit parameter examples

<?php
$str = 'one|two|three|four';

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

// negative limit (since PHP 5.1)
print_r(explode('|', $str, -1));
?>
Array
(
[0] => one
[1] => two|three|four
)
Array
(
[0] => one
[1] => two
[2] => three
)




Another example:

<?php
$text = "There are;many|variations of:passages of Lorem Ipsum available,but the/majority have\"suffered|alteration in some form,by injected humour,or randomised words which don't look even.slightly:believable./";

$delimiter = array(" ",",",".","'","\"","|","\\","/",";",":");
$replace = str_replace($delimiter, $delimiter[0], $text);
$explode = explode($delimiter[0], $replace);

echo '<pre>';
print_r($explode);
echo '</pre>';
// replaces many symbols in text, then explodes it
?>
Array
(
[0] => There
[1] => are
[2] => many
[3] => variations
[4] => of
[5] => passages
[6] => of
[7] => Lorem
[8] => Ipsum
[9] => available
[10] => but
[11] => the
[12] => majority
[13] => have
[14] => suffered
[15] => alteration
[16] => in
[17] => some
[18] => form
[19] => by
[20] => injected
[21] => humour
[22] => or
[23] => randomised
[24] => words
[25] => which
[26] => don
[27] => t
[28] => look
[29] => even
[30] => slightly
[31] => believable
[32] =>
[33] =>
)

LEAVE A REPLY

Please enter your comment!
Please enter your name here