Except where otherwise noted, the contents of this presentation are Copyright 2009 Marty Stepp and Jessica Miller.
Validation can be performed:
<form action="http://foo.com/foo.php" method="get"> <div> City: <input name="city" /> <br /> State: <input name="state" size="2" maxlength="2" /> <br /> ZIP: <input name="zip" size="5" maxlength="5" /> <br /> <input type="submit" /> </div> </form>
$city = $_REQUEST["city"]; $state = $_REQUEST["state"]; $zip = $_REQUEST["zip"]; if (!$city || strlen($state) != 2 || strlen($zip) != 5) { ?> <h2>Error, invalid city/state submitted.</h2> <?php }
"/^[a-zA-Z_\-]+@(([a-zA-Z_\-])+\.)+[a-zA-Z]{2,4}$/"
Scanner
, String
's split
method (CSE 143 sentence generator)"/abc/"
/
"abc"
:
"abc"
,
"abcdef"
,
"defabc"
,
".=.abc.=."
,
...
"fedcba"
,
"ab c"
,
"PHP"
,
...
.
.
matches any character except a \n
line break
"/.oo.y/"
matches
"Doocy"
,
"goofy"
,
"LooNy"
,
...
i
at the end of a regex (after the closing /
) signifies a case-insensitive match
"/mart/i"
matches
"Marty Stepp"
,
"smart fellow"
,
"WALMART"
,
...
|
, ()
, ^
, \
|
means OR
"/abc|def|g/"
matches "abc"
, "def"
, or "g"
()
are for grouping
"/(Homer|Marge) Simpson/"
matches "Homer Simpson"
or "Marge Simpson"
^
matches the beginning of a line; $
the end
"/^<!--$/"
matches a line that consists entirely of "<!--"
\
starts an escape sequence
/ \ $ . [ ] ( ) ^ * + ?
"/<br \/>/"
matches lines containing <br />
tags*
, +
, ?
*
means 0 or more occurrences
"/abc*/"
matches "ab"
, "abc"
, "abcc"
, "abccc"
, ..."/a(bc)*/"
matches "a"
, "abc"
, "abcbc"
, "abcbcbc"
, ..."/a.*a/"
matches "aa"
, "aba"
, "a8qa"
, "a!?_a"
, ...+
means 1 or more occurrences
"/a(bc)+/"
matches "abc"
, "abcbc"
, "abcbcbc"
, ..."/Goo+gle/"
matches "Google"
, "Gooogle"
, "Goooogle"
, ...?
means 0 or 1 occurrences
"/a(bc)?/"
matches "a"
or "abc"
{min,max}
{min,max}
means between min and max occurrences (inclusive)
"/a(bc){2,4}/"
matches "abcbc"
, "abcbcbc"
, or "abcbcbcbc"
{2,}
means 2 or more{,6}
means up to 6{3}
means exactly 3[]
[]
group characters into a character set; will match any single character from the set
"/[bcd]art/"
matches strings containing "bart"
, "cart"
, and "dart"
"/(b|c|d)art/"
but shorter[]
, many of the modifier keys act as normal characters
"/what[!*?]*/"
matches "what"
, "what!"
, "what?**!"
, "what??!"
, ..."/[ACGT]+/"
[start-end]
-
"/[a-z]/"
matches any lowercase letter"/[a-zA-Z0-9]/"
matches any lower- or uppercase letter or digit^
inside a character set negates it
"/[^abcd]/"
matches any character other than a, b, c, or d-
must be escaped to be matched
"/[+\-]?[0-9]+/"
matches an optional +
or -
, followed by at least one digit"/[ABCDF][+\-]?/"
\d
matches any digit (same as [0-9]
);
\D
any non-digit ([^0-9]
)
\w
matches any word character(same as
[a-zA-Z_0-9]
);
\W
any non-word char
\s
matches any whitespace character ( , \t
, \n
, etc.);
\S
any non-whitespace
"/\$\d{3,}\.\d{2}/"
/
, such as "/[AEIOU]+/"
function | description |
---|---|
preg_match(regex, string)
|
returns TRUE if string matches regex
|
preg_replace(regex, replacement, string)
|
returns a new string with all substrings that match regex replaced by replacement |
preg_split(regex, string)
|
returns an array of strings from given string broken apart using the given regex as the delimiter (similar to explode but more powerful)
|
# replace vowels with stars $str = "the quick brown fox"; $str = preg_replace("/[aeiou]/", "*", $str); # "th* q**ck br*wn f*x" # break apart into words $words = preg_split("/[ ]+/", $str); # ("th*", "q**ck", "br*wn", "f*x") # capitalize words that had 2+ consecutive vowels for ($i = 0; $i < count($words); $i++) { if (preg_match("/\\*{2,}/", $words[$i])) { $words[$i] = strtoupper($words[$i]); } } # ("th*", "Q**CK", "br*wn", "f*x")
\
must be escaped to \\
$state = $_REQUEST["state"]; if (!preg_match("/[A-Z]{2}/", $state)) { ?> <h2>Error, invalid state submitted.</h2> <?php }
preg_match
and well-chosen regexes allows you to quickly validate parameters