Specifications
Using PHP
P
ART I
94
In this chapter, we’ll discuss how you can use PHP’s string functions to format and manipulate
text. We’ll also discuss using string functions or regular expression functions to search (and
replace) words, phrases, or other patterns within a string.
These functions are useful in many contexts. You’ll often want to clean up or reformat user
input that is going to be stored in a database. Search functions are great when building search
engine applications (among other things).
In this chapter, we will cover
• Formatting strings
• Joining and splitting strings
• Comparing strings
• Matching and replacing substrings with string functions
• Using regular expressions
Example Application: Smart Form Mail
In this chapter, we’ll look at string and regular expression functions in the context of a Smart
Form Mail application. We’ll add these scripts to the Bob’s Auto Parts site we’ve been looking
at in the last few chapters.
This time, we’ll build a straightforward and commonly used customer feedback form for Bob’s
customers to enter their complaints and compliments, as shown in Figure 4.1. However, our
application will have one improvement over many you will find on the Web. Instead of email-
ing the form to a generic email address like feedback@bobsdomain.com,we’ll attempt to put
some intelligence into the process by searching the input for key words and phrases and then
sending the email to the appropriate employee at Bob’s company. For example, if the email
contains the word “advertising,” we might send the feedback to the Marketing department. If
the email is from Bob’s biggest client, it can go straight to Bob.
We’ll start with the simple script shown in Listing 4.1 and add to it as we go along.
LISTING 4.1 processfeedback.php—Basic Script to Email Form Contents
<?
$toaddress = “feedback@bobsdomain.com”;
$subject = “Feedback from web site”;
$mailcontent = “Customer name: “.$name.”\n”
.”Customer email: “.$email.”\n”
.”Customer comments: \n”.$feedback.”\n”;
$fromaddress = “webserver@bobsdomain.com”;
06 7842 CH04 3/6/01 3:41 PM Page 94