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.
| | 
			or | 
|---|---|
() | 
			grouping | 
^ | 
			start | 
$ | 
			end | 
* | 
			0 or more | 
|---|---|
+ | 
			1 or more | 
? | 
			0 or 1 | 
{min,max} | 
			between min and max | 
[abcde] | 
			one of those characters | 
|---|---|
[a-z] | 
			a character from a through z | 
\d | 
			digit | 
\s | 
			whitespace | 
Write a regular expression (slides) that would match the following kinds of patterns. You can use the site Rubular to test your regex.
/^[abcdf][+-]?$/i/^[acgt]+$/i/^\d{5}(-\d{4})?$//^\d{4}(-?\d{4}){3}$//^[-]?\d+(\.\d+)?$//^\$[1-9]\d{2,}\.\d{2}$//^[a-z]*[aeiou][a-z]*[aeiou][a-z]*$/i| 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)
			 | 
		
		The code for images.php displays all 
		
 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)
	
		
		
		
		(example output)
	
<?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
?>
		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)
	
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");
}
	$_COOKIES associative arrayisset function to see whether a given cookie name exists

		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:
	
middle.jpg once for each page view.
		prize.php is a travel site.
		We want roughly every 10th visitor to win a "free trip".
		(sample solution)
	
		
		
		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.
	
birthdays.phpbirthdays.php (alternate version)
		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:
	
stepp, reges, helenem, and jessica.  The password for stepp is booyah but you don't know the others...uwnetid, password, and loggedin.uwnetid to a name such as reges and set the cookie loggedin to true (such as in Firebug Cookies tab → Cookies → Create Cookie), it will think you are logged in even if you don't know any password.