HTML5 and CSS3 All-in-One For Dummies
Book image
Explore Book Buy On Amazon

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:



 
 explode


 

Using explode

n"; print_r($theArray); print "
n"; ?>

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:

image0.jpg
$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.

Regular expressions work the same in JavaScript and in PHP because both languages derived their regular expression tools from the older language Perl. (The part of stands for “Perl regular expression.”)

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:

image1.jpg


 
 preg_split.html


 

Using preg_split

n"; print_r($theArray); print "
n"; ?>

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.

About This Article

This article is from the book:

About the book author:

Andy Harris taught himself programming because it was fun. Today he teaches computer science, game development, and web programming at the university level; is a technology consultant for the state of Indiana; has helped people with disabilities to form their own web development companies; and works with families who wish to teach computing at home.

This article can be found in the category: