University of Washington CSE 154

Section 5: Regular Expressions, Validation; Cookies

Except where otherwise noted, the contents of this document are Copyright © 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.

Valid HTML5 Valid CSS

Recall: Regex Syntax Reference

| or
() grouping
^ start
$ end
special chars
* 0 or more
+ 1 or more
? 0 or 1
{min,max} between min and max
quantifiers
[abcde] one of those characters
[a-z] a character from a through z
\d digit
\s whitespace
character sets

Exercise : Regular Expressions (by Zack Cava)

Write a regular expression (slides) that would match the following kinds of patterns. You can use the site Rubular to test your regex.

Exercise Solution

Recall: Regular expressions in PHP

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)

Exercise : Regex Images (by Jamie Pell)

The code for images.php displays all icon JPG images in the images/ folder. Modify it using regular expressions so that it will display only image file namess that match each of the following patterns: (sample solution)

screenshot screenshot screenshot screenshot (example output)

Exercise Solution

<?php
$folder = "images";
$images = glob("$folder/*.jpg");

$regex = "/abbath/i";       # contain "abbath", case insensitive

foreach ($images as $image) {
	if (preg_match($regex, $image)) {
		?>
		<img src="<?= $image ?>" alt="an awesome picture" />
		<?php
	}
}

# begin with "abbath" and end with "cat", "dog", or "sheep"
# $regex = "/^$folder\/abbath(.*)(dog|cat|sheep).jpg$/";  
# $regex = "/[0-9].jpg$/";   # end in a number
# $regex = "/^[ab]{4}/i";    # start with 4 As/Bs
?>

Exercise : Sandwich Order (by Katlyn Edwards)

order.html allows the user to order a sandwich. Modify order-submit.php to use regular expressions to validate the order. If the order is invalid, kill the page with a brief error message. (sample solution)

Exercise Solution

Recall: Setting cookies in PHP

setcookie("name", "value");
...
$variable = $_COOKIE["name"];   # retrieve value of cookie later
setcookie("username", "martay");
setcookie("age", 19);
...
if (isset($_COOKIE["username"])) {
	$username = $_COOKIE["username"];
	print("Welcome back, $username.\n");
}

Exercise : Longcat Count (by Phil Sheperd)

top
bottom

Use a cookie (slides) to count the number of times the user has viewed longcat.php. Display this count to the user on the page, e.g. "You have visited 7 time(s)." (sample solution) For added challenge:

Exercise Solution

Exercise : Prize (by Michael Beswetherick)

prize.php is a travel site. We want roughly every 10th visitor to win a "free trip". (sample solution)

Exercise Solution

Exercise : Birthdays (by Phil Sheperd)

cake What day of the month were you born on? Let's check the box for everyone's day in birthdays.php. The form submits to itself, but if you leave the page and come back, it forgets what boxes were checked.

Exercise Solution

Exercise : Unsafe Cookie (by Shiny Yang)

The following page unsafe.php (source) is a mockup of a badly written user login page. The programmer implemented the login page poorly because of a misunderstanding about cookies. Without peeking at the source, figure out the following:

Exercise Solution