1. Trang chủ
  2. » Công Nghệ Thông Tin

PHP 5/MySQL Programming- P45 pot

5 188 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 143,2 KB

Nội dung

198 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 Table 6.2 summarizes the main regular expression elements. Operator Description Sample Pattern Matches Doesn’t match . any character .e\n but newline ^ beginning of string ^a apple banana $ end of string a$ banana apple [characters] any characters [abcABC] a d in braces [char range] describe range [a-zA-z] r 9 of characters \d any digit \d\d\d-\d\d\d\d 123-4567 the-thing \b word boundary \bthe\b the theater + one or more \d+ 1234 text occurrences of preceding character * zero or more [a-zA-z]\d* occurrences of preceding character {digit} repeat preceding \d{3}-\d{4} 123-4567 999-99-9999 character that many times |oroperator apple|banana apple, banana peach (pattern segment) store results in (^.).*/1$ gig, blab any other word pattern memory (any word returned with that starts numeric code and ends with same letter) TABLE 6.2 SUMMARY OF B ASIC R EGULAR E XPRESSION O PERATORS Note that square braces can contain either characters or a range of characters as indicated in the examples. To illustrate, I explain how the “/jpg$|gif$/” expression works. The expression “/jpg$|gif$/” matches on any string that ends with jpg or gif. • Slashes usually mark the beginning and end of regular expressions. The first and last characters of the expression are these slashes. TRICK • The pipe (|) character indicates or, so I’m looking for jpg or gif. • The dollar sign ( $) indicates the end of a string in the context of regular expressions, so jpg$ only matches on the value jpg if it’s at the end of a string. Regular expressions are extremely powerful if a bit cryptic. PHP supports a num- ber of special functions that use regular expressions in addition to preg_grep. Look in the online Help under “Regular Expression Functions—Perl compatible” for a list of these functions as well as details on how regular expressions work in PHP. If you find regular expressions baffling, you can usually find a string- manipulation function (or two) that does the same general job. Storing the Output Once the $imageFiles array is completed, the program uses the data to build an HTML index of all images and stores that data to a file. Since it’s been a bit since you’ve seen that code, I reproduce a piece of it here: foreach ($imageFiles as $currentFile){ $output .= <<<HERE <a href = $currentFile> <img src = “$currentFile” height = 50 width = 50> </a> HERE; } // end foreach //save the index to the local file system $fp = fopen(“imageIndex.html”, “w”); fputs ($fp, $output); fclose($fp); print “<a href = $dirName/imageIndex.html>image index</a>\n”; I use a foreach loop to step through each $imageFiles array element. I add the HTML to generate a thumbnail version of each image to a variable called $output. Finally, I open a file called imageIndex.html in the current directory for writing, put the value of $output to the file, and closed the file handle. Finally, I add a link to the file. 199 C h a p t e r 6 W o r k i n g w i t h F i l e s 200 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 You might be tempted to use a readFile() command to immediately view the contents of the file. I was. This may not work correctly, because the Web browser assumes the imageList.php directory is the current directory. Inside the pro- gram, I changed to another directory within the local file system, but the Web browser has no way of knowing that. The HTML was full of broken links when I did a readFile(), because all the relative links in the HTML page pointed towards files in another directory. When I add a link to the page instead, the Web browser itself can find all the images, because it’s sent to the correct directory. Working with Formatted Text Text files are easy to work with, but they are extremely unstructured. Sometimes you might want to impose a bit of formatting on a text file to work with data. You learn some more formal data management skills in the next couple of chapters, but with a few simple tricks you can do quite a lot with plain text files. Introducing the mailMerge.php Program To illustrate how to use text files for basic data storage, I created a simple mail- merge program. The results are shown in Figure 6.9. You can see that the same letter was used repeatedly, each time with a different name and e-mail address. The name and e-mail information was pulled from a file. HINT FIGURE 6.9 The program creates several form letters from a list of names and e-mail addresses. Determining a Data Format The data file (shown in Figure 6.10) for this program is simply a file created in Notepad. Each line consists of a name and an e-mail address, separated by a tab character. This particular format (one line per record, tab-separated fields) is called a tab- delimited file. Because you can easily create a tab-delimited file in a text editor, spreadsheet, or any other kind of program, such files are popular. It’s also quite easy to use another character as a separator. Spreadsheet programs often save in a comma-delimited format ( CSV for comma-separated values ) but string data does not work well in this format because it might already have embedded commas. Examining the mailMerge.php Code The basic strategy for the mailMerge.php program is very simple. Take a look at the code and you might be surprised: <html> <head> <title>Mailing List</title> </head> <body> <form> TRICK 201 C h a p t e r 6 W o r k i n g w i t h F i l e s FIGURE 6.10 The data file for this program was created in Notepad. <? //Simple Mail merge //presumes tab-delimited file called maillist.dat $theData = file(“maillist.dat”); foreach($theData as $line){ $line = rtrim($line); print “<h3>$line</h3>”; list($name, $email) = split(“\t”, $line); print “Name: $name”; $message = <<<HERE TO: $email: Dear $name: Thanks for being a part of the spam afficionado forum. You asked to be notified whenever a new recipe was posted. Be sure to check our Web site for the delicious ‘spam flambe with white sauce and cherries’ recipe. Sincerely, Sam Spam, Host of Spam Afficionados. HERE; print “<pre>$message</pre>”; } // end foreach ?> </body> </html> Loading Data with the file() Command The first step is loading the data into the form. Instead of using the file pointer technique, I use a special shortcut. The file() command takes a filename and automatically loads that file into an array. Each line of the file becomes an 202 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 . format because it might already have embedded commas. Examining the mailMerge .php Code The basic strategy for the mailMerge .php program is very simple. Take a look at the code and you might be surprised: <html> <head> <title>Mailing. compatible” for a list of these functions as well as details on how regular expressions work in PHP. If you find regular expressions baffling, you can usually find a string- manipulation function. of the file. I was. This may not work correctly, because the Web browser assumes the imageList .php directory is the current directory. Inside the pro- gram, I changed to another directory within

Ngày đăng: 07/07/2014, 02:20

TỪ KHÓA LIÊN QUAN