Solution 10

Main | Arrays | Practice 8 | Solution 8 | Associative Arrays | Practice 9 | Solution 9 | Regular Expressions | Practice 10 | Solution 10 | More Regular Expressions | Practice 11 | Solution 11

Here is the solution to practice 10:

function email_good($email="") {
  $regex = '^[A-Za-z0-9_-]+@[A-Za-z0-9_-]+.[A-Za-z0-9_-]+.*';
  return eregi($regex ,$email);
}

function check_input($email,  $date, $url, $u_name) {
  $str = "";
  if (!email_good($email)) {
    $str .= "Your email address does not look correct<br>";
  }
  if (!ereg('[0-9]{4}-[0-9]{2}-[0-9]{2}', $date)) {
    $str .= "Your date does not look correct<br>";
  }
  if (!url_good($url)) {
    $str .= "Your URL does not look correct<br>";
  }
  return $str;
}

The code that you completed for this exercise is close to being a realistic application. It has the following properties which you should note:

  • Modular Design
  • Takes advantage of reserved variables so the file can be renamed
  • Uses a table to line up the form
  • Functions to convert between formats that are good for storage and what is user-friendly

I recommend that you use it as a basis for other applications that need online forms. The function show_form() could be modified to ask different types of questions, and process_input could be modified to email the data, write the data to a file, or insert the data into a MySQL database. We have already covered the first of these options and we will cover the last two in our next class.


jfulton [at] member.fsf.org
22 Aug 2013