BooleanSearch.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /**
  3. * Contains Boolean search from the search form.
  4. */
  5. class FreshRSS_BooleanSearch {
  6. private $raw_input = '';
  7. private $searches = array();
  8. public function __construct($input) {
  9. $input = trim($input);
  10. if ($input == '') {
  11. return;
  12. }
  13. $this->raw_input = $input;
  14. $input = preg_replace('/:&quot;(.*?)&quot;/', ':"\1"', $input);
  15. $splits = preg_split('/\b(OR)\b/i', $input, -1, PREG_SPLIT_DELIM_CAPTURE);
  16. $segment = '';
  17. $ns = count($splits);
  18. for ($i = 0; $i < $ns; $i++) {
  19. $segment = $segment . $splits[$i];
  20. if (trim($segment) == '' || strcasecmp($segment, 'OR') === 0) {
  21. $segment = '';
  22. } else {
  23. $quotes = substr_count($segment, '"') + substr_count($segment, '&quot;');
  24. if ($quotes % 2 === 0) {
  25. $segment = trim($segment);
  26. if ($segment != '') {
  27. $this->searches[] = new FreshRSS_Search($segment);
  28. }
  29. $segment = '';
  30. }
  31. }
  32. }
  33. $segment = trim($segment);
  34. if ($segment != '') {
  35. $this->searches[] = new FreshRSS_Search($segment);
  36. }
  37. }
  38. public function searches() {
  39. return $this->searches;
  40. }
  41. public function __toString() {
  42. return $this->getRawInput();
  43. }
  44. public function getRawInput() {
  45. return $this->raw_input;
  46. }
  47. }