Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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 <nhalstead00@gmail.com>
- */
- $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 . "`";
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement