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.
list()
Function: It is generally bad style to keep
referencing values from arrays by indicies like $data[1]
and $data[4]
instead you should use list()
to unpack them into appropriately named
variables
| |
or |
---|---|
() |
grouping |
^ |
start |
$ |
end |
. |
wildcard |
* |
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)
<?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 = "/^{$folder}\/[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"); }
$_COOKIE
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)