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