The character classes are created by placing characters within square backets ([]).
Character | Shortcut | Meaning |
---|---|---|
[0-9] | \d | Any digit |
[\f\r\t\n\v] | \s | Any whitespace |
[A-Za-z0-9] | \w | Any word character |
[^0-9] | \D | Not a digit (^ is a negation) |
[^\f\r\t\n\v] | \S | Not whitespace |
[^A-Za-z0-9] | \W | Not a word character |
\b | Word boundary.This allows to to distinguish whole words. | |
\B | Non-word boundary |
<html> <head> <title>PHP</title> <meta Name="Author" Content="Hann So"> </head> <body> <p> <?php if (isset($_POST['submit'])) { process_form(); } else { display_form();// display form for the first time } function display_form() { echo <<<HTML <form action = "$_SERVER[SCRIPT_NAME]" method="post"> Regular Expression Pattern (include the delimiter):<br /> <input type="text" name="pattern" size="70" value="/^(\d{5})(-\d{4})?$/" /><br /> Text:<br /> <input type="text" name="text" size="70" value="95015-2887" /><br /> <input type="submit" name="submit" value="Check" /> </form> <table border> <tr bgcolor="lightcyan"> <th align="left">Type of Input</th> <th align="left">Regular Expression</th> </tr> <tr> <td>Zip Code</td> <td><b>/^(\d{5})(-\d{4})?$/</b> <br />(^(\d{5}) starts with 5 digits, (-\d{4})?$ means dash with 4 digits at the end. ? means 0 or 1 as zip code can have 5 or 9 digits (last 4 are optional)) <br />or<br /> <b>/^\d{5}((-|\s)?\d{4})?$/</b> </td> </tr> <tr bgcolor="lightyellow"> <td>E-mail</td> <td><b>/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/</b> <br />(^[\w.-]+ starts with 1 or more letters, numbers, underscore, period and a dash. \.[A-Za-z]{2,6} ends with one period and between 2 and 6 letters to account for .com, .travel, etc.) <br />or<br /> <b>/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6})$/</b> </td> </tr> <tr> <td>U.S. phone number</td> <td><b>/^\(?\d{3}\)?-?\s*\d{3}\s*-?\d{4}$/</b> </td> </tr> <tr bgcolor="lightyellow"> <td>Credit card number</td> <td><b>/^((4\d{3})|(5[1-5]\d{2})|(6011))-?\d{4}-?\d{4}-?\d{4}|3[4,7]\d{13}$/</b> </td> </tr> <tr> <td>Social Security number</td> <td><b>/^\d{3}-?\d\d-?\d{4}$/</b> </td> </tr> <tr bgcolor="lightyellow"> <td>URL</td> <td><b>/^((http\https|ftp)://)?([\w-])+(\.)(\w){2,4}([\w/+=%&_.~?-]*)$/</b> </td> </tr> </table> HTML; } function process_form() { // trim the strings $pattern = stripslashes(trim($_POST['pattern'])); $text = trim($_POST['text']); echo "The result of checking <br /><font color=red>$pattern</font><br /> against<br /><font color=red>$text</font><br /> is:<br />"; // match the pattern if (preg_match($pattern, $text, $matches)) { echo "$pattern was matched. <br />"; } else { echo "$pattern was not matched. <br />"; } echo "<br />The matches array contains:<br />"; foreach ($matches as $key =>$values) { echo $key, " => ", $values, "<br />"; } echo "<br /><a href=\"$_SERVER[SCRIPT_NAME]\">Check again?</a>"; } ?> </p> </body> </html> |