function_search.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: function_search.php 36278 2016-12-09 07:52:35Z nemohou $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. function searchkey($keyword, $field, $returnsrchtxt = 0) {
  12. $srchtxt = '';
  13. if($field && $keyword) {
  14. if(preg_match("(AND|\+|&|\s)", $keyword) && !preg_match("(OR|\|)", $keyword)) {
  15. $andor = ' AND ';
  16. $keywordsrch = '1';
  17. $keyword = preg_replace("/( AND |&| )/is", "+", $keyword);
  18. } else {
  19. $andor = ' OR ';
  20. $keywordsrch = '0';
  21. $keyword = preg_replace("/( OR |\|)/is", "+", $keyword);
  22. }
  23. $keyword = str_replace('*', '%', addcslashes($keyword, '%_'));
  24. $srchtxt = $returnsrchtxt ? $keyword : '';
  25. foreach(explode('+', $keyword) as $text) {
  26. $text = trim(daddslashes($text));
  27. if($text) {
  28. $keywordsrch .= $andor;
  29. $keywordsrch .= str_replace('{text}', $text, $field);
  30. }
  31. }
  32. $keyword = " AND ($keywordsrch)";
  33. }
  34. return $returnsrchtxt ? array($srchtxt, $keyword) : $keyword;
  35. }
  36. function highlight($text, $words, $prepend) {
  37. $text = str_replace('\"', '"', $text);
  38. foreach($words AS $key => $replaceword) {
  39. $text = str_replace($replaceword, '<highlight>'.$replaceword.'</highlight>', $text);
  40. }
  41. return "$prepend$text";
  42. }
  43. function bat_highlight($message, $words, $color = '#ff0000') {
  44. if(!empty($words)) {
  45. $highlightarray = explode(' ', $words);
  46. $sppos = strrpos($message, chr(0).chr(0).chr(0));
  47. if($sppos !== FALSE) {
  48. $specialextra = substr($message, $sppos + 3);
  49. $message = substr($message, 0, $sppos);
  50. }
  51. bat_highlight_callback_highlight_21($highlightarray, 1);
  52. $message = preg_replace_callback("/(^|>)([^<]+)(?=<|$)/sU", 'bat_highlight_callback_highlight_21', $message);
  53. $message = preg_replace("/<highlight>(.*)<\/highlight>/siU", "<strong><font color=\"$color\">\\1</font></strong>", $message);
  54. if($sppos !== FALSE) {
  55. $message = $message.chr(0).chr(0).chr(0).$specialextra;
  56. }
  57. }
  58. return $message;
  59. }
  60. function bat_highlight_callback_highlight_21($matches, $action = 0) {
  61. static $highlightarray = array();
  62. if($action == 1) {
  63. $highlightarray = $matches;
  64. } else {
  65. return highlight($matches[2], $highlightarray, $matches[1]);
  66. }
  67. }
  68. ?>