RE: [PHP] Filters and sanitizing a regexp
| From: | Boyd, Todd M. | Date: | Thu, 25 Sep 2008 18:23:38 +0000 |
| Subject: | RE: [PHP] Filters and sanitizing a regexp | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-280935@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Frank Stanovcak [mailto:blindspotpro@comcast.net]
> Sent: Thursday, September 25, 2008 12:41 PM
> To: php-general@lists.php.net
> Subject: [PHP] Filters and sanitizing a regexp
>
> Is it possible to use the php filter function to sanitize a regular
> expression such as to return just the date part of a string that may
be
> passed by an nonobservant user?
>
> "#\d\d/\d\d/\d\d\d\d#"
>
> input would be something like
> as02/05/2008df
>
> I want to use the filter to give me just the date should some oaf not
> leave
> the date field as just digits.
Using preg_match and numbered groups, you could re-build the variable
like this:
<?php
$var = "as02/05/2008df";
$matches = array();
preg_match("#(\d{2})\D*(\d{2})\D*(\d{4})#", $var, $matches);
if(count($matches) != 4) {
echo "Invalid data<br />";
} else {
$filtered = $matches[1] . $matches[2] . $matches[3];
echo $filtered;
}
?>
HTH,
Todd Boyd
Web Programmer