PHP Tutorial :: Exercises (XIV)

PHP Exercise #29

Matching a phone number

Write a regular expression that matches a phone number either if it has or doesn't has parentheses around the provincial code, and
it has each of its parts separated by spaces, hyphens or dots. The regular expression should match with phone numbers written like
this:

(718) 498-1043

(718) 498 1043

718 498 1043

718 498-1043

718-498-1043

718.498.1043

// This regular expression matches with:
// an optional literal (
// three digits
// an optional literal )
// a hyphen, a space or a dot
// three digits
// a hyphen, a space or a dot
// four digits
// ^ and $ match the expression only with strings that solely contain
// a phone number and not any additional text

^\(?\d{3}\)?[- \.]\d{3}[- \.]\d{4}$

PHP Exercise #30

Matching letters and numbers

Write a expression that checks that the input sent from a form field named 'username' contains only letters and numbers.

PHP Exercise #32

Matching hyperlinks

Write a program that retrieves a remote webpage and prints a list of the hyperlinks that are on that page, by simply searching for
the links that are like <a href="http://www.example.com">Example page</a>, and not worrying about links that have any
other attributes in the <a> tag.