PHP Split a string by string using explode method

php explode — Split a string by string

Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter.

Example 1

<?php
$example  = “example1 example2 example3 example4 example5 example6″;
$result = explode(” “, $example);
echo $result[0]; // example1
echo $result[1]; // example2
?>

This is simple method to explode & trim whitespaces from the exploded elements
array_map(‘trim’,explode(“,”,$str));

example:
$str=”one  ,two  ,       three  ,  four    “;
print_r(array_map(‘trim’,explode(“,”,$str)));

Output:
Array ( [0] => one [1] => two [2] => three [3] => four )

Leave a Reply

Your email address will not be published. Required fields are marked *

(Required)

Proudly powered by WordPress   Premium Style Theme by www.gopiplus.com