note 29745 added to function.preg-match

From: Date: Mon, 24 Feb 2003 01:38:21 +0000
Subject: note 29745 added to function.preg-match
Groups: php.notes 
Request: Send a blank email to php-notes+get-44413@lists.php.net to get a copy of this message
preg_match to verify if you've got some "reasonable" text in a form: I have a form with a TEXTAREA statement. The inserted text comes back upon submission and is placed in an array. Here's a test to see if the user has entered in something reasonable. if ((!preg_match("/[A-z0-9_\-\!\@\.]/", $formvalues_report_array["text"])) & (preg_match("/(\r|\n|\s\t)/", $formvalues_report_array["text"]))) { echo "The text you entered does not appear be a reasonable set of alphanumeric characters."; } Someone, I imagine, can post a single logical statement to do both tests, but the tests are: !preg_match("/[A-z0-9_\-\!\@\.]/," "text string") If we don't see anything from A to Z, a to z, 0 to 9, or the characters '_', '-', '!', '@', '.' then this returns TRUE. Note, add your own special characters as needed. and preg_match("/(\r|\n|\s\t)/", "text string") We are looking for newline, carriage return, tabs, and spaces. If we see these, then this is true. Thus, if the user has entered characters that are 'junk', hit a few spaces and returns, then we return the message saying this doesn't look like reasonable text. -- http://www.php.net/manual/en/function.preg-match.php http://master.php.net/manage/user-notes.php?action=edit+29745 http://master.php.net/manage/user-notes.php?action=delete+29745 http://master.php.net/manage/user-notes.php?action=reject+29745

« previous php.notes (#44413) next »