Validating Data: Checking for Strings


PHP supports regular expressions, which let you check text for matches. For instance, in this next example, we'll insist that the text the user enters contains the string "PHP" using the case-insensitive regular expression '/php/i'. (We don't have the space here to cover regular expressions in detail, but they're great for checking text for matchestake a look at http://www.perldoc.com/perl5.6/pod/perlre.html for more on how to create them.) In PHP, you can use the preg_match function to use regular expressions; here's how you can insist that the user-entered text contains "PHP":

 function validate_data() {     global $errors;     if(!preg_match('/php/i', $_REQUEST["Text"])){         $errors[] = "<FONT COLOR='RED'>Please include \"PHP\" in your         text.</FONT>";     } } 

The whole application, phpregularexpressions.php, appears in Example 6-12.

Example 6-12. Requiring string input, phpregularexpressions.php
 <HTML>     <HEAD><TITLE>Using Regular Expressions</TITLE></HEAD>     <BODY>         <CENTER>                 <H1>Using Regular Expressions</H1>         <?php             $errors = array();             if(isset($_REQUEST["seen_already"])){                 validate_data();                 if(count($errors) != 0){                     display_errors();                     display_welcome();                 }                 else {                     process_data();                 }             }             else {                 display_welcome();             }             function validate_data()             {                 global $errors;                 if(!preg_match('/php/i', $_REQUEST["Text"])){                     $errors[] = "<FONT COLOR='RED'>Please include \"PHP\" in                     your text.</FONT>";                 }             }             function display_errors()             {                 global $errors;                 foreach ($errors as $err){                     echo $err, "<BR>";                 }             }             function process_data()             {                 echo "You said: ";                 echo $_REQUEST["Text"];             }             function display_welcome()             {                 echo "<FORM METHOD='POST'                     ACTION='phpregularexpressions.php'>";                 echo "Say something about PHP:";                 echo "<BR>";                 echo "<INPUT NAME='Text' TYPE='TEXT'>";                 echo "<BR>";                 echo "<BR>";                 echo "<INPUT TYPE='SUBMIT' VALUE='Submit'>";                 echo "<INPUT TYPE='HIDDEN' NAME='seen_already'                     VALUE='hidden_data'>";                 echo "</FORM>";             }         ?>         </CENTER>     </BODY> </HTML> 

You can see the results if you omit "PHP" somewhere in your text in Figure 6-15.

Figure 6-15. Searching for "PHP" in text data.




    Spring Into PHP 5
    Spring Into PHP 5
    ISBN: 0131498622
    EAN: 2147483647
    Year: 2006
    Pages: 254

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net