13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

4<br />

String Manipulation <strong>and</strong> Regular<br />

Expressions<br />

IN THIS CHAPTER, WE DISCUSS HOW YOU can use <strong>PHP</strong>’s string functions to format <strong>and</strong><br />

manipulate text.We also discuss using string functions or regular expression functions to<br />

search (<strong>and</strong> replace) words, phrases, or other patterns within a string.<br />

These functions are useful in many contexts.You often may want to clean up or<br />

reformat user input that is going to be stored in a database. Search functions are great<br />

when building search engine applications (among other things).<br />

Key topics covered in this chapter include<br />

n Formatting strings<br />

n Joining <strong>and</strong> splitting strings<br />

n Comparing strings<br />

n Matching <strong>and</strong> replacing substrings with string functions<br />

n Using regular expressions<br />

Creating a Sample Application: Smart Form<br />

Mail<br />

In this chapter, you use string <strong>and</strong> regular expression functions in the context of a Smart<br />

Form Mail application.You then add these scripts to the Bob’s Auto Parts site you’ve<br />

been building in preceding chapters.<br />

This time, you build a straightforward <strong>and</strong> commonly used customer feedback form<br />

for Bob’s customers to enter their complaints <strong>and</strong> compliments, as shown in Figure 4.1.<br />

However, this application has one improvement over many you will find on the <strong>Web</strong>.<br />

Instead of emailing the form to a generic email address like feedback@example.com,<br />

you’ll attempt to put some intelligence into the process by searching the input for key<br />

words <strong>and</strong> phrases <strong>and</strong> then sending the email to the appropriate employee at Bob’s<br />

company. For example, if the email contains the word advertising, you might send the

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!