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.

128 Chapter 4 String Manipulation <strong>and</strong> Regular Expressions<br />

Table 4.4 Summary of Special Characters Used in POSIX Regular Expressions<br />

Outside Square Brackets<br />

Character Meaning<br />

\ Escape character<br />

^<br />

Match at start of string<br />

$ Match at end of string<br />

. Match any character except newline (\n)<br />

| Start of alternative branch (read as OR)<br />

( Start subpattern<br />

) End subpattern<br />

* Repeat zero or more times<br />

+ Repeat one or more times<br />

{ Start min/max quantifier<br />

} End min/max quantifier<br />

? Mark a subpattern as optional<br />

Table 4.5 Summary of Special Characters Used in POSIX Regular Expressions Inside<br />

Square Brackets<br />

Character Meaning<br />

\ Escape character<br />

^<br />

NOT, only if used in initial position<br />

- Used to specify character ranges<br />

Putting It All Together for the Smart Form<br />

There are at least two possible uses of regular expressions in the Smart Form application.<br />

The first use is to detect particular terms in the customer feedback.You can be slightly<br />

smarter about this by using regular expressions. Using a string function, you would have<br />

to perform three different searches if you wanted to match on "shop", "customer<br />

service", or "retail".With a regular expression, you can match all three:<br />

shop|customer service|retail<br />

The second use is to validate customer email addresses in the application by encoding<br />

the st<strong>and</strong>ardized format of an email address in a regular expression.The format includes<br />

some alphanumeric or punctuation characters, followed by an @ symbol, followed by a<br />

string of alphanumeric <strong>and</strong> hyphen characters, followed by a dot, followed by more<br />

alphanumeric <strong>and</strong> hyphen characters <strong>and</strong> possibly more dots, up until the end of the<br />

string, which encodes as follows:<br />

^[a-zA-Z0-9_\-.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-.]+$

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

Saved successfully!

Ooh no, something went wrong!