Previous Section  < Day Day Up >  Next Section

B.8 Exercises

  1. Write a regular expression that flexibly matches a U.S. phone number whether or not it has parentheses around the area code and has its parts separated by spaces, hyphens, or periods. The regular expression should match phone numbers written like this:

    • (718) 498-1043

    • (718) 498 1043

    • 718 498 1043

    • 718 498-1043

    • 718-498-1043

    • 718.498.1043

  2. What would you add to a validate_form( ) function to check that a submitted form field named username contains only letters and numbers? Use if( ), preg_match( ), and a regular expression.

  3. Starting with the code from Example 10-3, write a program that retrieves the weather page for your ZIP Code and parses that page with a regular expression to get the current temperature.

  4. Write a program that retrieves a remote web page and prints a list of the hyperlinks in that page. Just look for links that look like this: <a href="http://www.example.com">The Example Page</a>. Don't worry about links with other attributes in the <a> tag.

    Previous Section  < Day Day Up >  Next Section