Tải bản đầy đủ (.pdf) (5 trang)

PHP 5/MySQL Programming- P37 pps

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (81.44 KB, 5 trang )

Finding a Substring with substr()
The algorithm’s behavior depends on the first character of each word. I need to
know all the rest of the word without the first character. The
substr() function
is useful for getting part of a string. It requires three parameters.
• The string you want to get a piece from
• Which character you want to begin with (starting with
0 as usual)
• How many characters you want to extract
I got the first letter of the word with this line:
$firstLetter = substr($theWord, 0, 1);
It gets one letter from $theWord starting at the beginning of the word (position 0).
I stored that value in the
$firstLetter variable.
It’s not much more complicated to get the rest of the word:
$restOfWord = substr($theWord, 1, strlen($theWord) -1);
Once again, I need to extract values from $theWord. This time I begin at character
1 (which humans would refer to as the
second character
). I don’t know directly
how many characters to get, but I can calculate it. I should grab one less charac-
ter than the total number of characters in the word. The
strlen() function is per-
fect for this operation, because it returns the number of characters in any string.
I can calculate the number of letters I need with
strlen($theWord) – 1. This new
decapitated word is stored in the
$restOfWord variable.
Using strstr() to Search for
One String Inside Another
The next task is to determine if the first character of the word is a vowel. You can


take a number of approaches to this problem, but perhaps the easiest is a search-
ing function. I created a string with all the vowels (
“aeiouAEIOU”) and then I
searched for the existence of the
$firstLetter variable in the vowel string.
The
strstr() function is perfect for this task. It takes two parameters: the string
you are looking for (given the adorable name
haystack
in the online documenta-
tion) and the string you are searching in (the
needle
).
To search for the value of the
$firstLetter variable in the string constant
“aeiouAEIOU”, I used the following line:
if (strstr(“aeiouAEIOU”, $firstLetter)){
158
P
H
P
5
/M
y
S
Q
L
P
r
o

g
r
a
m
m
i
n
g
f
o
r
t
h
e
A
b
s
o
l
u
t
e
B
e
g
i
n
n
e
r

The strstr() function returns the value FALSE if the needle was not found in the
haystack. If the needle was found, it returns the position of the needle in the
haystack parameter. In this case, I’m really concerned only whether
$firstLetter
is found in the list of variables. If so, it’s a vowel, which changes the way I modify
the word.
Using the Concatenation Operator
Most of the time in PHP you can use string interpolation to combine string val-
ues. However, sometimes you need a formal operation to combine strings. The
process of combining two strings is called
concatenation.
(I love it when simple
ideas have complicated names.) The period (
.) is PHP’s concatenation operator.
If a word in pig Latin begins with a vowel, it should end with the string
“way”.
I used string concatenation to make this work:
$newWord = $theWord . “way”;
When the word begins with a consonant, the formula for creating the new word
is slightly more complicated, but is still performed with string concatenation:
$newWord = $restOfWord . $firstLetter . “ay”;
Recent testing has shown that the concatenation method of building strings is
dramatically faster than interpolation. If speed is an issue, you might want to use
string concatenation rather than string interpolation.
Finishing the Pig Latin Program
Once I created the new word, I added it and a trailing space to the $newPhrase
variable. When the foreach loop has finished executing, $newPhrase contains the
pig Latin translation of the original phrase.
Translating Between Characters and ASCII Values
Although it isn’t necessary in the pig Latin program, the Word Search program

requires the ability to randomly generate a character. I do this by randomly gen-
erating an ASCII value and translating that number to the appropriate character.
(ASCII is the code used to store characters as binary numbers in the computer’s
memory.) The
ord() function is useful in this situation. The uppercase letters are
represented in ASCII by numbers between 65 and 90.
TRICK
159
C
h
a
p
t
e
r
5
B
e
t
t
e
r
A
r
r
a
y
s
a
n

d
S
t
r
i
n
g
H
a
n
d
l
i
n
g
To get a random uppercase letter, I can use the following code:
$theNumber = random(65, 90);
$theLetter = ord($theNumber);
Returning to the Word Search Creator
The Word Search program is stored in three files. First, the user enters a word
list and puzzle information into an HTML page. This page calls the main
wordFind.php program, which analyzes the word list, creates the puzzle, and
prints it out. Finally, the user has the opportunity to print an answer key, which
is created by a simple PHP program.
Getting the Puzzle Data from the User
The wordFind.html page is the user’s entry point into the word find system. This
page is a standard HTML form with a number of basic elements:
<html>
<head>
<title>Word Puzzle Maker</title>

</head>
<body>
<center>
<h1>Word Puzzle Maker</h1>
<form action = “wordFind.php”
method = “post”>
<h3>Puzzle Name</h3>
<input type = “text”
name = “name”
value = “My Word Find”>
height: <input type = “text”
name = “height”
value = “10”
size = “5”>
width: <input type = “text”
name = “width”
value = “10”
size = “5”>
<br><br>
160
P
H
P
5
/M
y
S
Q
L
P

r
o
g
r
a
m
m
i
n
g
f
o
r
t
h
e
A
b
s
o
l
u
t
e
B
e
g
i
n
n

e
r
<h3>Word List</h3>
<textarea rows=10 cols=60 name = “wordList”></textarea>
<br><br>
Please enter one word per row, no spaces
<br>
<input type=”submit” value=”make puzzle”>
</form>
</center>
</body>
</html>
The form’s action property points to the wordFind.php program, which is the pri-
mary program in the system. I used the
post method to send data to the program
because I expect to send large strings to the program. The
get method allows only
small amounts of data to be sent to the server.
The form features basic text boxes for the puzzle name, height, and width. This
data determines how the puzzle is built. The
wordList text area is expected to
house a list of words, which create the puzzle.
Setting Up the Response Page
The bulk of the work in the wordFind system happens in the wordFind.php page.
This program has a small amount of HTML to set the stage, but the vast bulk of
this file is made up of PHP code.
<html>
<head>
<title>
Word Find

</title>
</head>
<body>
<?
// word Find
// by Andy Harris, 2003
// for PHP/MySQL programming for the Absolute Beginner
// Generates a word search puzzle based on a word list
// entered by user. User can also specify the size of
// the puzzle and print out an answer key if desired
161
C
h
a
p
t
e
r
5
B
e
t
t
e
r
A
r
r
a
y

s
a
n
d
S
t
r
i
n
g
H
a
n
d
l
i
n
g
Notice the comments at the beginning of the code. Since this program’s code is
a little more involved than most of the programs you have seen in this book, I
decided to comment it more carefully. My comments here basically lay out the
plan for this program.
It’s a really good idea to add comments to your programs so you can more easily
determine what they do. You’ll be amazed how little you understand your own
code after you’ve been away from it for a couple of days. Good comments can
make it much easier to maintain your code, and make it easier for others to fix
and improve your programs later.
Working with the Empty Data Set
For testing purposes, I wrote the Word Search PHP page before I worried about the
HTML. For that reason, I simply added default values for a word list and for the

other main variables that determine the board’s layout (height, width, and
name). In a production version of the program, I don’t expect the PHP code to
ever be called without an HTML page, but I left the default values in place so you
could see how they work.
if ($wordList = = NULL){
//make default puzzle
$word = array(
“ANDY”,
“HEATHER”,
“LIZ”,
“MATT”,
“JACOB”
);
$boardData = array(
width => 10,
height => 10,
name => “Generic Puzzle”
);
This code builds two arrays, which define the entire program. The $word array
holds the list of words to hide in the puzzle, and
$boardData is an associative
array holding critical information about how the board is to be created.
Of course, I don’t expect to use these values, because this program usually is
called from an HTML form, which generates the values. The next section of code
fills up these variables if the program is called from the appropriate form.
162
P
H
P
5

/M
y
S
Q
L
P
r
o
g
r
a
m
m
i
n
g
f
o
r
t
h
e
A
b
s
o
l
u
t
e

B
e
g
i
n
n
e
r

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×