Introducing the Word Search Program Creator By the end of this chapter you can create a fun program that generates word search puzzles.. The user enters a series of words into a list box
Trang 1In this chapter you learn some important skills that improve your work with data You learn about some more-sophisticated ways to work with arrays
and how to manage text information with more flair Specifically, you learn how to do these things:
• Manage arrays with the foreach loop
• Create and use associative arrays
• Extract useful information from some of PHP’s built-in arrays
• Build basic two-dimensional arrays
• Build two-dimensional associative arrays
• Break a string into smaller segments
• Search for one string inside another
Better Arrays
and String Handling
5
C H A P T E R
Trang 2Introducing the Word
Search Program Creator
By the end of this chapter you can create a fun program that generates word search puzzles The user enters a series of words into a list box, as shown in Figure 5.1
The program then tries to generate a word search based on the user’s word list (It isn’t always possible, but the program can usually generate a puzzle.) One pos-sible solution for the word list shown in Figure 5 1 is demonstrated in Figure 5.2
If desired, the program can also generate an answer key based on the puzzle This capability is shown in Figure 5.3
The secret to the word find game (and indeed most computer programs) is the way the data is handled Once I determined a good scheme for working with the data
in the program, the actual programming wasn’t too tough
134
g r
s o
l u
g in
e r
FIGURE 5.1
The user enters a
list of words and a
size for the finished
puzzle.
Trang 3Using the foreach Loop
to Work with an Array
that makes it even easier to step through each array element
135
l i
FIGURE 5.2
This puzzle
contains all the
words in the list.
FIGURE 5.3
Here’s the answer
key for the puzzle.
Trang 4Introducing the foreach.php Program
The HTML page is generated by surprisingly simple code:
<html>
<head>
<title>Foreach Demo</title>
</head>
<body>
<?
$list = array(“alpha”, “beta”, “gamma”, “delta”, “epsilon”);
print “<ul>\n”;
foreach ($list as $value){
print “ <li>$value</li>\n”;
} // end foreach
print “</ul>\n”;
?>
</body>
</html>
136
g r
s o
l u
g in
e r
FIGURE 5.4
Although it looks
just like normal
HTML, this page
was created with
an array and a
foreach loop.
Trang 5All the values in the list are created in the $listvariable using the arrayfunction.
foreachloop steps through the $listarray as many times as necessary Each time
foreach ($list as $value){
print “ <li>$value</li>\n”;
} // end foreach
for ($i = 0; $i < length($list); $i++);
$value = $list[$i];
print “ <li>$value</li>\n”;
} // end for loop
The main difference between a foreach loop and a for loop is the presence of the index variable ($i in this example) If you’re using a foreach loop and need
to know the current element’s index, use the key() function.
value of an array Since this is a common task, knowing how to use the foreach
loop is an important skill As you learn some other kinds of arrays, you see how
Creating an Associative Array
PHP is known for its extremely flexible arrays You can easily generate a number
of interesting and useful array types in addition to the ordinary arrays you’ve
already made One of the handiest types is called an associative array
While regular arrays rely on numeric indices, an associative array has a string
index Figure 5.5 shows a page created with two associative arrays
T R I C K
137
l i