We covered PHP implode in a previous article, where we used implode to make a string out of an array. PHP has another function that does the reverse, explode. Explode takes a string and makes it into an array. Just like with implode, explode has some quirks.
The syntax for explode is: explode( [delimiter], [string], [limit] );
The limit is optional. Let's use a $teststring to show you the various examples of using explode.
$teststring = "this is a test string."; $testarray = explode(" ", $teststring);
We separate the array where ever we have a space in the string, and it looks like this.
Let's change the string slightly by putting some more spaces between the words and before the period.
$teststring2 = "A double space and a triple space here ."; $testarray2 = explode(" ", $teststring2);
Extra space matters in a string, if your using a space as a delimiter.
NO DELIMITER or NO SPACES
Let's start, changing the delimiter. What if we don't have a space, or we forget the delimiter?
$teststring3 = "A double space and a triple space here ."; $testarray3 = explode("", $teststring3);
and it looks like this.
Not what we were expecting. You need to have at least a space for the delimiter, or it will throw a warning, and you won't have the array you're looking for. You can use any delimiter, for example a comma, or colon, to separate the words of the string, but you need a delimiter with explode.
Let's move on to the optional "limit" parameter. We'll use 3 as the limit, or the number of array elements we want.
$teststring4 = "Lets look for more string elements."; $testarray4 = explode(" ", $teststring4, 3 );
We asked for 3 array elements starting at the beginning of the string. The last element of the array will contain the rest of the string, but suppose we just want to see the first three words. Well, we can count backwards like so,
$teststring5 = "Lets look for more string elements."; $testarray5 = explode(" ", $teststring5, -3);
We count back 3 spaces and we get what we wanted. A negative limit takes elements out of the array.
USING MANY DELIMITERS WITH ONE STRING
Let's look at a real-life problem, and see how we can use explode(). We'll assign a variable to a typical log entry, like so:
$teststring6 = "127.0.0.1[25/Oct/2014:23:21:39 -0400]'GET /scripts/explode.php HTTP/1.1' 200 396";
We want to break this string into an array, so we can parse the date, minutes, seconds, and file path. The explode function will not take an array directly, but we can replace the different delimiters with one delimiter, and then explode the teststring that way.
We'll make the first element, of an array of delimiters, found in our teststring a space, and replace all the other delimiters in the teststring with spaces.
Here's the code:
// The teststring, our typical log entry $teststring6 = "127.0.0.1[25/Oct/2014:23:21:39 -0400]'GET /scripts/explode.php HTTP/1.1' 200 396"; // Our array of delimiters found in the teststring that we want to replace with the first delimiter, a space $delimiterArr = array(" ", "[", "]", ":", "'"); $replace = str_replace($delimiterArr, $delimiter, $teststring6); // Here comes the explode, we make an array element at each space boundary $testarray6 = explode($delimiter, $replace);
Let's take a look
We've replaced a comma and space, a [, a ], a :, and a single quote with a space with the str_replace function, and then exploded the replace string at every space.
As you can see the explode() function can be quite useful in parsing strings for needed information.
(For those of you who would like your strings and arrays to appear like you see in this article can download my free chk class from newchk.com. There's a link to the site on the left sidebar.)