Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * This allows for a Search Request to be Parsed to extract key
  5. * values from it while leaving all other text within the request.
  6. *
  7. * @author Noah Halstead <nhalstead00@gmail.com>
  8. */
  9.  
  10. $str = 'parent:11 student:344,234,22 status:paid Something Else to Search';
  11. $p = [];
  12. $s = [];
  13. $o = [];
  14.  
  15. $re = '/([a-zA-Z]*)([\s]{0,1}):([a-zA-Z0-9,]*)(\s|)/m';
  16. preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
  17. foreach($matches as $m){
  18. $mm = strtolower($m[1]);
  19. switch($mm){
  20. case "parent":
  21. $pt = $m[3];
  22. $pt = explode(',', $pt);
  23. $pt = array_map('trim', $pt);
  24. $pt = array_map('intval', $pt);
  25. $p = array_merge($p, $pt);
  26. $str = str_replace($m[0], "", $str);
  27. break;
  28. case "student":
  29. $st = $m[3];
  30. $st = explode(',', $st);
  31. $st = array_map('trim', $st);
  32. $st = array_map('intval', $st);
  33. $s = array_merge($s, $st);
  34. $str = str_replace($m[0], "", $str);
  35. break;
  36. case "status":
  37. $status = $m[3];
  38. $status = explode(',', $status);
  39. $status = array_map('trim', $status);
  40. $o = array_merge($o, $status);
  41. $str = str_replace($m[0], "", $str);
  42. break;
  43. default:
  44. // Unknown Key
  45. break;
  46. }
  47. }
  48.  
  49. 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 . "`";
  50.  
  51.  
  52. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement