Last active
June 13, 2019 03:57
-
-
Save nhalstead/9d3cde58387428ba9de13d692f031061 to your computer and use it in GitHub Desktop.
Search Syntax Formulator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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) . " Parent: " . json_encode($p) . " Status Codes: ". json_encode($o) . "<br> Also Search with: `" . $str . "`"; | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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