Specifications

Using PHP
P
ART I
94
In this chapter, well discuss how you can use PHPs string functions to format and manipulate
text. Well 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. Youll 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, well look at string and regular expression functions in the context of a Smart
Form Mail application. Well add these scripts to the Bobs Auto Parts site weve been looking
at in the last few chapters.
This time, well build a straightforward and commonly used customer feedback form for Bobs
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,well 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 Bobs company. For example, if the email
contains the word advertising, we might send the feedback to the Marketing department. If
the email is from Bobs biggest client, it can go straight to Bob.
Well start with the simple script shown in Listing 4.1 and add to it as we go along.
LISTING 4.1 processfeedback.phpBasic 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