Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*---------------------------------------------------------------------------*/
- define('FILTER_SEP_AND', '+');
- define('FILTER_SEP_OR', ',');
- define('FILTER_ESCAPE', '\\');
- function parse_filter($source) {
- $filter = array(array('')); $escape = false;
- $length = strlen($source); $index = 0;
- $group = 0; $sub_group = 0;
- while ($index++ < $length) {
- $char = $source[$index - 1];
- // Toggle escape status:
- if ($char == FILTER_ESCAPE) {
- $escape = !$escape;
- if ($escape) continue;
- // Handle unescaped:
- } else if (!$escape) {
- if ($char == FILTER_SEP_AND) {
- $filter[] = array('');
- $group++; $sub_group = 0;
- continue;
- } else if ($char == FILTER_SEP_OR) {
- $filter[$group][] = '';
- $sub_group++;
- continue;
- }
- } else {
- $escape = false;
- }
- $filter[$group][$sub_group] .= $char;
- }
- return $filter;
- }
- function build_query($filter) {
- $query = "(";
- foreach ($filter as $index => $group) {
- if ($index) $query .= "\n)\nAND (";
- foreach ($group as $sub_index => $sub_group) {
- $query .= "\n\t";
- if ($sub_index) $query .= "OR ";
- $query .= "value = " . trim($sub_group);
- }
- }
- $query .= "\n)";
- return $query;
- }
- /*---------------------------------------------------------------------------*/
- header('content-type: text/plain');
- $source = 'one, two \\+ three, four + five, six\\, \\"seven\\" \\\\+ eight';
- $filter = parse_filter($source);
- $query = build_query($filter);
- echo "Turn this: {$source}\n\nInto this:\n";
- echo $query;
- /*---------------------------------------------------------------------------*/
- ?>
Add Comment
Please, Sign In to add comment