FILTER_VALIDATE_REGEXP in filter array not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gosteen
Forum Newbie
Posts: 3
Joined: Thu Oct 06, 2011 3:59 pm

FILTER_VALIDATE_REGEXP in filter array not working

Post by gosteen »

Hello this is my first post. I'm new to PHP and am working through the w3 tutorials. I decided to try a regular expression test but seem to be having and issue :banghead: Any help for a newb would be greatly appreciated.

Here is my html
<html>
<body>
<form action="filter.php" method="get">
Name: <input type="text" name = "name" /><br />
Age: <input type="text" name = "age" /><br />
Email: <input type="text" name="email" /><br />

<input type="submit" />
</form>
</body>
</html>
And here is my filter.php code

Code: Select all

<?php
$filters = array
  (
  "name" => array
    (
    "filter"=>FILTER_VALIDATE_REGEXP,
    "options" => array("regexp" "/^[A-Z]{1}[a-z]{1,15}\s[A-Z]{1}[a-z]{1,15}/")
    ),
  "age" => array
    (
    "filter"=>FILTER_VALIDATE_INT,
    "options"=>array
      (
      "min_range"=>1,
      "max_range"=>120
      )
    ),
  "email"=> FILTER_VALIDATE_EMAIL,
  );

$result = filter_input_array(INPUT_GET, $filters);
if (!$result["name"])
  {
  echo("Must give First and last name.<br />");
  }
if (!$result["age"])
  {
  echo("Age must be a number between 1 and 120.<br />");
  }
elseif(!$result["email"])
  {
  echo("E-Mail is not valid.<br />");
  }
else
  {
  echo("User input is valid");
  }
?>

I get this when I view the following in Chrome:

Code: Select all

Server error
The website encountered an error while retrieving http://192.168.1.133/filter.php?name=Gene+Osteen&age=22&email=gosteen%40hops.com. It may be down for maintenance or configured incorrectly.
Here are some suggestions:
Reload this webpage later.
HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.
The following simple PHP works fine

Code: Select all

<?php
$string = "Gene Osteen";

var_dump(filter_var($string, FILTER_VALIDATE_REGEXP,
array("options"=>array("regexp"=>"/^[A-Z]{1}[a-z]{1,15}\s[A-Z]{1}[a-z]{1,15}/"))))
?>
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: FILTER_VALIDATE_REGEXP in filter array not working

Post by Eric! »

I've never seen that function until now, but off hand I see you're missing a comma in this part of the code where I inserted it after "regexp":

Code: Select all

"options" => array("regexp","/^[A-Z]{1}[a-z]{1,15}\s[A-Z]{1}[a-z]{1,15}/")),
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: FILTER_VALIDATE_REGEXP in filter array not working

Post by AbraCadaver »

Also, just FYI, the filter will fail for people with names like Shawn McKenzie, McKenzie Smith, Tip O'Neill, Mary Smith-Johnson, etc...
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
gosteen
Forum Newbie
Posts: 3
Joined: Thu Oct 06, 2011 3:59 pm

Re: FILTER_VALIDATE_REGEXP in filter array not working

Post by gosteen »

:oops: A comma. I've been programming for 22 years. It is amazing how your eyes see what they expect to see.

Eric, thanks for the quick response.

AbraCadavra, Yes i was not meaning for this to be a real test, just to get a real test working. I appreciate the input.
gosteen
Forum Newbie
Posts: 3
Joined: Thu Oct 06, 2011 3:59 pm

Re: FILTER_VALIDATE_REGEXP in filter array not working

Post by gosteen »

AbraCadaver, Sorry for the misspelling.
Post Reply