PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/swishe/src/lmbSwishHighlite.class.php

http://github.com/limb-php-framework/limb
PHP | 60 lines | 44 code | 5 blank | 11 comment | 0 complexity | c203d4a57fd9740c0f48756b9882b8ba MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-2.0
  1. <?php
  2. /*
  3. * Limb PHP Framework
  4. *
  5. * @link http://limb-project.com
  6. * @copyright Copyright &copy; 2004-2009 BIT(http://bit-creative.com)
  7. * @license LGPL http://www.gnu.org/copyleft/lesser.html
  8. */
  9. /**
  10. * @package swishe
  11. * @version $Id$
  12. */
  13. class lmbSwishHighlite
  14. {
  15. protected $marker_left;
  16. protected $marker_right;
  17. function __construct($marker_left, $marker_right)
  18. {
  19. $this->marker_left = $marker_left;
  20. $this->marker_right = $marker_right;
  21. }
  22. function process($query, $result)
  23. {
  24. $marked = $result;
  25. $items = $this->_parseQuery($query);
  26. foreach($items as $item)
  27. {
  28. $marked = preg_replace('~(?<!' . preg_quote($this->marker_left) . ')(' . preg_quote($item) . ')~i',
  29. $this->marker_left . '$1' . $this->marker_right,
  30. $marked);
  31. }
  32. return $marked;
  33. }
  34. protected function _parseQuery($query)
  35. {
  36. $query = strtolower($query);
  37. $query = preg_replace('~\s+~', ' ', $query);
  38. $query = str_replace('(', '', $query);
  39. $query = str_replace(')', '', $query);
  40. $query = str_replace('"', '', $query);
  41. $query = str_replace("'", '', $query);
  42. $query = str_replace('*', '', $query);
  43. $query = str_replace(' and ', ' ', $query);
  44. $query = str_replace(' or ', ' ', $query);
  45. $query = str_replace(' not ', ' ', $query);
  46. $query = str_replace('!', '', $query);
  47. $query = str_replace('&', '', $query);
  48. $query = str_replace('|', '', $query);
  49. $query = trim($query);
  50. $items = array_unique(explode(' ', $query));
  51. arsort($items);
  52. return $items;
  53. }
  54. }