Skip to content

Instantly share code, notes, and snippets.

@nhalstead
Last active June 13, 2019 03:57
Show Gist options
  • Save nhalstead/9d3cde58387428ba9de13d692f031061 to your computer and use it in GitHub Desktop.
Save nhalstead/9d3cde58387428ba9de13d692f031061 to your computer and use it in GitHub Desktop.
Search Syntax Formulator
<?php
/**
* This allows for a Search Request to be Parsed to extract key
* values from it while leaving all other text within the request.
*
* @author Noah Halstead <[email protected]>
*/
$str = 'parent:11 student:344,234,22 status:paid Something Else to Search';
$p = [];
$s = [];
$o = [];
$re = '/([a-zA-Z]*)([\s]{0,1}):([a-zA-Z0-9,]*)(\s|)/m';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
foreach($matches as $m){
$mm = strtolower($m[1]);
switch($mm){
case "parent":
$pt = $m[3];
$pt = explode(',', $pt);
$pt = array_map('trim', $pt);
$pt = array_map('intval', $pt);
$p = array_merge($p, $pt);
$str = str_replace($m[0], "", $str);
break;
case "student":
$st = $m[3];
$st = explode(',', $st);
$st = array_map('trim', $st);
$st = array_map('intval', $st);
$s = array_merge($s, $st);
$str = str_replace($m[0], "", $str);
break;
case "status":
$status = $m[3];
$status = explode(',', $status);
$status = array_map('trim', $status);
$o = array_merge($o, $status);
$str = str_replace($m[0], "", $str);
break;
default:
// Unknown Key
break;
}
}
echo "<br>Student: " . json_encode($s) . "&nbsp;&nbsp;&nbsp;&nbsp;Parent: " . json_encode($p) . "&nbsp;&nbsp;&nbsp;&nbsp;Status Codes: ". json_encode($o) . "<br> Also Search with: `" . $str . "`";
?>
Student: [344,234,22] Parent: [11] Status Codes: ["paid"]
Also Search with: `Something Else to Search`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment