HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [221]
4. Look up the distance in the distance array.
A 2D array requires two indices. The first indicates the row, and the second indicates the column.
5. Print out the result.
After you get the data, it’s pretty easy to print out.
Breaking a String into an Array
Many times, it can be useful to break a string into an array, especially when reading input from a file.
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.
Creating 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 (see Figure 4-8 for the output):
PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
Using explode
$theString = “PARC (Palo Alto Research Center) was one of the single most important hubs of invention for modern computing”;
$theArray = explode(“ “, $theString);
print “
\n”;\n”;print_r($theArray);
print “
?>
Figure 4-8:
A string exploded into an array.
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. split uses regular expressions to split a string into an array, which can make it a bit slower than explode.
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.
If you need a refresher on regular expressions, check Book IV, Chapter 6. Regular expressions work the same in JavaScript and in PHP because both languages derived their regular expression tools from the older language perl. (The preg part of preg_split 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 both of these, you could do the following (see Figure 4-9 for the output):
PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
Using preg_split
$theString = ”joe@somebody.net”;
$theArray = preg_split(”/[@\.]/”, $theString);
print ”
\n”;\n”;print_r($theArray);
print ”
?>
Recall that regular expressions are encased in the slash character, and the square braces indicate one of a number of options. I want to split on