Except where otherwise noted, the contents of this document are Copyright 2012 Marty Stepp, Jessica Miller, and Victoria Kirst. All rights reserved. Any redistribution, reproduction, transmission, or storage of part or all of the contents in any form is prohibited without the author's expressed written permission.
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>
htmlspecialchars
function
htmlspecialchars
|
returns an HTML-escaped version of a string |
$text = "<p>hi 2 u & me</p>";
$text = htmlspecialchars($text); # "<p>hi 2 u & me</p>"
$city = $_POST["city"]; $state = $_POST["state"]; $zip = $_POST["zip"]; if (!$city || strlen($state) != 2 || strlen($zip) != 5) { print "Error, invalid city/state/zip submitted."; }
/^[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"
\
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!?xyz__9a"
, ...+
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^
and $
^
represents the beginning of the string or line; $
represents the end
/Jess/
matches all strings that contain Jess
; /^Jess/
matches all strings that start with Jess
; /Jess$/
matches all strings that end with Jess
; /^Jess$/
matches the exact string "Jess"
only
/^Mart.*Stepp$/
matches "MartStepp"
, "Marty Stepp"
, "Martin D Stepp"
, ... "Marty Stepp stinks"
or "I H8 Martin Stepp"
/PATTERN/
matches "text"
, we really mean that it matches any string that contains that text)
[]
[]
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 given regex as delimiter (like explode but more powerful)
|
$state = $_POST["state"]; if (!preg_match("/^[A-Z]{2}$/", $state)) { print "Error, invalid state submitted."; }
preg_match
and regexes help you to validate parameters# 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 \\