How to Break Strings into Arrays in PHP for HTML5 and CSS3 Programming
Many times, it can be useful in PHP to break a string into an array, especially when reading input from a file for HTML5 and CSS3 programming. Here are the two different ways of doing this:
explode: explode takes one parameter as a delimiter and splits the string into an array based upon that one parameter.
preg_split: If you require regular expressions, using preg_split is the way to go. split allows you to take complicated chunks of text, look for multiple different delimiters stored in a regular expression, and break it into an array based on the delimiters you specify.
explode works well with comma-separated value (CSV) files and the like, where all the parameters you wish to break the text on are the same. preg_split works better for when there are many different parameters that you wish to break the text on or when the parameter you’re looking for is complex.
How to create arrays with explode
Array creation with explode is very straightforward:
explode(" ", $theString);
The first value is the parameter on which you’re splitting up the string. The second value is the string you would like to split into an array. In this example, the string would be split up on each space. You can put anything you want as the split parameter.
So, if you have the string that you want to store each word as a value in, enter the following code:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>explode</title> </head> <body> <h1>Using explode</h1> <?php $theString = "Twas brillig and the slithy toves"; $theArray = explode(" ", $theString); print "<pre> n"; print_r($theArray); print "</pre> n"; ?> </body> </html>
The delimiter can be anything you want. If you’re dealing with a CSV file, where each value is separated by a comma, your explode method might look like this:
$theArray = explode(",", $theString);
Creating arrays with preg_split
preg_split is a bit more complicated. preg_split uses regular expressions to split a string into an array, which can make it a bit slower than explode.
preg_split looks exactly like explode, but instead of one character inside quotations, you can cram all the characters you want to split on into brackets inside the quotations, or you can use a complicated regular expression to determine how the values will split.
An instance where you’d want to use preg_split instead of explode could be when processing an e-mail address. A basic e-mail address has dots (.) and an at sign (@). So, to split on either of these characters, you could do the following:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>preg_split.html</title> </head> <body> <h1>Using preg_split</h1> <?php $theString = "firstname.lastname@example.org"; $theArray = preg_split("/[@.]/", $theString); print "<pre>n"; print_r($theArray); print "</pre>n"; ?> </body> </html>
Recall that regular expressions are encased in the slash character, and the square braces indicate one of a number of options. You want to split on either the at sign or the period. Remember to specify the period with . because an ordinary period means “any character.”
preg_split works well for timestamps, e-mail addresses, and other things where there’s more than just one unique delimiter that you wish to split the string on.
Earlier versions of PHP had a function called split. It was much like the preg_split function, but it used a different regular expression syntax. Hardly anybody used it, and it is no longer supported. Use explode for simple patterns and preg_split when you need the power of regular expressions.