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

PHP 5/MySQL Programming- P45 pot

5 188 0
Tài liệu đã được kiểm tra trùng lặp

Đ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

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Operator Description Sample Pattern Matches Doesn’t match but newline [characters] any characters [abcABC] a d in braces [char range] describe range [a-zA-z] r 9 of characters occurrence

Trang 1

g r

s o

l u

g in

e r

Table 6.2 summarizes the main regular expression elements

Operator Description Sample Pattern Matches Doesn’t match

but newline

[characters] any characters [abcABC] a d

in braces

[char range] describe range [a-zA-z] r 9

of characters

occurrences of preceding character

occurrences of preceding character

{digit} repeat preceding \d{3}-\d{4} 123-4567 999-99-9999

character that many times

(pattern segment) store results in (^.).*/1$ gig, blab any other word

with same letter)

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

• Slashes usually mark the beginning and end of regular expressions The first and last characters of the expression are these slashes

T R I C K

Trang 2

• The pipe (|) character indicates or,so I’m looking for jpgor gif

• The dollar sign ($) indicates the end of a string in the context of regular

expressions, so jpg$only matches on the value jpgif 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 $imageFilesarray 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.htmlin the current directory for writing,

put the value of $outputto the file, and closed the file handle Finally, I add a link

to the file

199

Trang 3

g r

s o

l u

g in

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

H I N T

FIGURE 6.9

The program

creates several

form letters from

a list of names and

e-mail addresses.

Trang 4

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, separated fields) is called a tab-delimited file Because you can easily create a tab-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.phpprogram is very simple Take a look at the

code and you might be surprised:

<html>

<head>

<title>Mailing List</title>

</head>

<body>

<form>

T R I C K

201

FIGURE 6.10

The data file for this

program was

created in Notepad.

Trang 5

//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

g r

s o

l u

g in

e r

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

TỪ KHÓA LIÊN QUAN