Beruflich Dokumente
Kultur Dokumente
CS380
CS380
both
best mix of convenience and security,
CS380 but requires most effort to program
An example form to be
validated
<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>
HTML
CS380
?>
CS380
CS380
Regular expressions
8
[a-z]at
#cat, rat, bat
[aeiou]
[a-zA-Z]
[^a-z]
#not a-z
[[:alnum:]]+
#at least one alphanumeric char
(very) *large #large, very very very large
(very){1, 3}
#counting very up to 3
^bob
#bob at the beginning
com$
#com at the end
PHPRegExp
CS380
POSIX
Perl regular expressions
Delimiters
9
/[a-z]/at
#cat, rat, bat
#[aeiou]#
/[a-zA-Z]/
~[^a-z]~
#not a-z
/[[:alnum:]]+/
#at least one alphanumeric char
#(very) *#large
#large, very very very large
~(very){1, 3}~
#counting very up to 3
/^bob/
#bob at the beginning
/com$/
#com at the end
/http:\/\
// #http://#
#better readability
PHPRegExp
CS380
/abc/
Wildcards
11
CS380
| means OR
CS380
CS380
Quantifiers: *, +, ?
14
15
More quantifiers:
{min,max}
CS380
Character sets: []
16
17
CS380
18
CS380
Escape sequences
19
char
CS380
20
function
preg_match(regex,
string)
preg_replace(regex,
replacement, string)
preg_split(regex, string)
description
returns TRUE if string
matches regex
returns a new string with
all substrings that match
regex replaced by
replacement
returns an array of
strings from given string
broken apart using the
given regex as the
21
Regular expressions
example
echo preg_match ('/test/', "a test of preg_match");
echo preg_match ('/tutorial/', "a test of preg_match
");
$matchesarray[0] = "http://www.tipsntutorials.com/"
$matchesarray[1] = "http://"
$matchesarray[2] = "www.tipsntutorials.com/"
preg_match ('/(http://)
(.*)/', "http://www.tipsntutorials.com/", $matchesar
ray)
PHP
CS380
22
Regular expressions
example
# 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")
PHP
CS380
23
CS380
CS380