Page 1 of 1

FILTER_VALIDATE_REGEXP in filter array not working

Posted: Thu Oct 06, 2011 9:52 pm
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}/"))))
?>

Re: FILTER_VALIDATE_REGEXP in filter array not working

Posted: Thu Oct 06, 2011 11:00 pm
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}/")),

Re: FILTER_VALIDATE_REGEXP in filter array not working

Posted: Fri Oct 07, 2011 10:09 am
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...

Re: FILTER_VALIDATE_REGEXP in filter array not working

Posted: Fri Oct 07, 2011 11:36 am
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.

Re: FILTER_VALIDATE_REGEXP in filter array not working

Posted: Fri Oct 07, 2011 11:37 am
by gosteen
AbraCadaver, Sorry for the misspelling.