Hi all!
I would like to ask your advice on a script that I wrote. As you can surely
see from my script below, I really need help as I am totally new to PHP.
Regular expressions are my worst nightmare because I don't have a
background in software programming: I'm just kidding around here! Please
post your suggestions. Thanks.
Problem
-------
HTML form to filter records from an SQL database on a DATE field.
Search expressions are of the form:
DATE (should be transformed into = DATE)
>= DATE (or other operator)
BETWEEN DATE_1 AND DATE_2
The variable named $order_date contains the search expression submitted by
the HTML form.
Here's my script
----------------
// Regex pattern for date, e.g. DD/MM/YY or MM-DD-YYYY
$date_pat = '[0-9]+(\/|-)[0-9]+(\/|-)[0-9]+';
// Regex pattern for SQL operators
$op_pat = '<>|<=|>=|!=|<|>|=';
// First, see if there is at least one date in $_POST['order_date']
$count = preg_match_all('/'.$date_pat.'/', $order_date, $matches);
if ($count) {
for ($i = 0; $i <= $count - 1; $i++) {
// Quote dates using PEAR::DB
$quoted_date[$i] = $db->quote($matches[0][$i]);
}
}
// Then see if search string is of type BETWEEN DATE_1 AND DATE_2
$count = preg_match_all('/between\s'.$date_pat.'\sand\s'.$date_pat.'/i',
$order_date, $matches);
if ($count) {
// Yes it's a BETWEEN...AND. We assume that we have two valid dates
$date_filter = ' BETWEEN '.$quoted_date[0].' AND '.$quoted_date[1];
} else {
// No. Then check if we have a search string like '>= DATE'
$count = preg_match_all('/'.$op_pat.'/', $order_date, $matches);
if ($count) {
// Yes, then use operator and quoted date
$date_filter = ' '.$matches[0][0].' '.$quoted_date[0];
} else {
// No, then we just use something like ' = DATE'
$date_filter = ' = '.$quoted_date[0];
}
}
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php