explode();
Because some things are better apart.
Around the office, we’re all a bit nerdy and get pretty excited over new bits of code. So when we came across php’s “explode();” function, we were understandably excited. Here’s a common setup for it:
<?php
//store the comma-separated list in a variable called $string
$string = "Fe, Fi, Fo, Fum";
//explode $string by comma and store in a new variable called $strings
$strings = explode(",", $string);
?>
Based on the above example, when the string is exploded all the values between the separators are broken apart and stored into an array. From there, each piece can be called individually. Using $strings[3] would bring us “Fum” while $string[0] would be “Fe“.
Here’s a breakdown:
- $strings[0] = Fe
- $strings[1] = Fi
- $strings[2] = Fo
- $strings[3] = Fum
The ability to be able to manage a string like this is, well, incredibly handy. We like it. A lot. Some might say too much. But those are strange people.
Of course, the separator doesn’t have to be a comma. It could be a period, quote, or even a word, like “and”.
Don’t believe us? Try using “we like pies and we like cookies and we like ice cream and we like root beer” as the string with “and” as the separator (all without the quotation marks) below.
Or try your own string of text and separator to see how it explodes!








































I know you’re really excited about being able to split a string (you should try Googling regular expressions!) but your little toy is open to cross-site scripting.
It’s true, and we’re keeping an eye on that. Hopefully no one abuses our fun little tutorial else we’ll have to take it down. And that’d be sad.
PS — Regular expressions are fun!