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.

Finding Substrings with Regular Expressions<br />

129<br />

The subexpression ^[a-zA-Z0-9_\-.]+ means “start the string with at least one letter,<br />

number, underscore, hyphen, or dot, or some combination of those.” Note that when a<br />

dot is used at the beginning or end of a character class, it loses its special wildcard meaning<br />

<strong>and</strong> becomes just a literal dot.<br />

The @ symbol matches a literal @.<br />

The subexpression [a-zA-Z0-9\-]+ matches the first part of the hostname including<br />

alphanumeric characters <strong>and</strong> hyphens. Note that you slash out the hyphen because it’s a<br />

special character inside square brackets.<br />

The \. combination matches a literal dot (.).We are using a dot outside character<br />

classes, so we need to escape it to match only a literal dot.<br />

The subexpression [a-zA-Z0-9\-\.]+$ matches the rest of a domain name, including<br />

letters, numbers, hyphens, <strong>and</strong> more dots if required, up until the end of the string.<br />

A bit of analysis shows that you can produce invalid email addresses that will still<br />

match this regular expression. It is almost impossible to catch them all, but this will<br />

improve the situation a little.You can refine this expression in many ways.You can, for<br />

example, list valid top-level domains (TLDs). Be careful when making things more<br />

restrictive, though, because a validation function that rejects 1% of valid data is far more<br />

annoying than one that allows through 10% of invalid data.<br />

Now that you have read about regular expressions, you’re ready to look at the <strong>PHP</strong><br />

functions that use them.<br />

Finding Substrings with Regular Expressions<br />

Finding substrings is the main application of the regular expressions you just developed.<br />

The two functions available in <strong>PHP</strong> for matching POSIX-style regular expressions are<br />

ereg() <strong>and</strong> eregi().The ereg() function has the following prototype:<br />

int ereg(string pattern, string search, array [matches]);<br />

This function searches the search string, looking for matches to the regular expression<br />

in pattern. If matches are found for subexpressions of pattern, they will be stored in<br />

the array matches, one subexpression per array element.<br />

The eregi() function is identical except that it is not case sensitive.<br />

You can adapt the Smart Form example to use regular expressions as follows:<br />

if (!eregi(‘^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$’, $email)) {<br />

echo "That is not a valid email address.".<br />

Please return to the previous page <strong>and</strong> try again.";<br />

exit;<br />

}<br />

$toaddress = “feedback@example.com”; // the default value<br />

if (eregi(“shop|customer service|retail”, $feedback))

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

Saved successfully!

Ooh no, something went wrong!