PHP for the World Wide Web (Visual QuickStart Guide)

I l @ ve RuBoard

There are two functions built in to PHP expressly for the purpose of matching a pattern within a string: ereg() and eregi (). The only difference between the two is that ereg() treats patterns as case-sensitive whereas eregi() is case-insensitive, making it less particular. The latter is generally recommend for common use, unless you need to be more explicit (perhaps for security purposes, as with passwords). Both functions will be evaluated to TRUE if the pattern is matched, FALSE if it is not. Here are two different ways to use these functions:

ereg("pattern", "string");

Or:

$Pattern = "pattern"; $String = "string"; eregi($Pattern, $String);

Throughout the rest of the chapter, I will assign the pattern to a variable, as in the second example above, to draw more attention to the pattern itselfthe heart of any regular expression.

To demonstrate matching a pattern, you'll create a new HandleForm.php that works in conjunction with form.html from Chapter 7, Using Arrays (Script 8.1). The HandleForm.php script will validate the submitted email address.

Script 8.1. The original form.html was designed to accept an e-mail address and user comments, as well as the user 's first and last names .

To match a pattern using eregi:

  1. Create a new HandleForm.php script in your text editor.

  2. Begin with the standard HTML and PHP header (Script 8.2):

    Script 8.2. This script makes a very practical use of regular expressions to validate that the user not only entered an email address in the HTML form, but actually submitted a valid one.

    <HTML> <HEAD> <TITLE>Using Regular Expressions </TITLE> <BODY> <?php /* This page receives and handles the data generated by "form.html". */

  3. Create the necessary conditionals.

    if (($Array["FirstName"]) AND ($Array["LastName"])) { $Array["Name"] = $Array["FirstName"] . " " . $Array["LastName"]; } else { print ("Please enter your first and last names.<BR>\n"); }

    You could use a regular expression to make sure that they have entered a valid first name, consisting only of alphabetic characters , but ensuring that the variable has a value is sufficient. Then, if the user entered a value for both the first and last names, the names will be merged into one element and assigned to the array. But, if either is omitted, a request will be printed.

  4. Next, you'll establish the pattern for matching an email address:

    $Pattern = ".+@.+\..+";

    This will be the pattern to match a valid email address. Although it is quite simple, it will work just fine (a more elaborate one will be developed later in the chapter though for comparison).

    The first step in the pattern says that an email address must begin with at least one character (.+). Second, there is the at symbol (@), which is required in any email address. Third, the pattern insists upon at least one more character. Fourth, an email address must include a period. Finally, there must be at least one more character concluding the string (it cannot end with a period).

  5. Use the pattern to check for a match with the submitted email address.

    if (eregi($Pattern, $Array["Email"])) { print ("Your information has been received!<BR>\n"); } else { print ("Please enter a valid email address!\n"); }

    In order to use the matching function, eregi(), you will feed it the pattern as defined above and the $Array["Email"] variable which comes from form.html. If PHP finds the pattern within the variable, the condition will be TRUE and the proper message will be printed. If it does not find the pattern within the variable, a request will be made to enter a valid email address. As email addresses are case-insensitive, you will use eregi() and not ereg().

  6. Save your script, upload it to your server, and test it in your Web browser (Figures 8.2, 8.3 and 8.4).

    Figure 8.2. The form.html page takes the user input and sends it on to be checked using regular expressions in the HandleForm.php page (Figure 8.3).

    Figure 8.3. Using eregi(), if the user enters an email address that matches the defined pattern, the HandleForm.php script will create this message. Compare this to Figure 8.4.

    Figure 8.4. Here I supplied php@DMCinsights, which is not a valid email address, resulting in the error message displayed.

Tip

Although it demonstrates dedication to programming to learn how to write and execute your own regular expressions, numerous working examples (including variants of the email matching pattern) are available already from the various PHP Web sites, referenced in Appendix C, PHP Resources.

Tip

For demonstration purposes, I have assigned a pattern to a variable and then referred to the variable in the eregi() function. You could, however, place your pattern directly within the function, if you wanted, like so:

if (eregi(".+@.+\..+", $Array ["Email"])) {...

Tip

Form validation, similar to what you have just coded above, can also be achieved as a client-side process using JavaScript and regular expressions. Depending upon your circumstances, you may use regular expressions in one language or the other or both.

I l @ ve RuBoard

Категории