From f4de82bcdafba51e4eed9cae6b2d3e5375ffd115 Mon Sep 17 00:00:00 2001 From: xue <> Date: Tue, 9 May 2006 12:11:38 +0000 Subject: --- demos/quickstart/protected/pages/Search.php | 53 +++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 demos/quickstart/protected/pages/Search.php (limited to 'demos/quickstart/protected/pages/Search.php') diff --git a/demos/quickstart/protected/pages/Search.php b/demos/quickstart/protected/pages/Search.php new file mode 100644 index 00000000..c7bdce89 --- /dev/null +++ b/demos/quickstart/protected/pages/Search.php @@ -0,0 +1,53 @@ +IsPostBack && strlen($text = $this->search->getText()) > 0) + { + $search = $this->getApplication()->getModule("search"); + $this->results->setDataSource($search->find($text)); + $this->results->dataBind(); + } + } + + public function highlightSearch($text) + { + $words = str_word_count($text, 1); + $keys = str_word_count(strtolower($this->search->getText()),1); + $where = 0; + $t = count($words); + for($i = 0; $i<$t; $i++) + { + if($this->containsKeys(strtolower($words[$i]), $keys)) + { + $words[$i] = ''.$words[$i].''; + $where = $i; + break; + } + } + + $min = $where - 15 < 0 ? 0 : $where - 15; + $max = $where + 15 > $t ? $t : $where + 15; + $subtext = array_splice($words, $min, $max-$min); + $prefix = $min == 0 ? '' : '...'; + $suffix = $max == $t ? '' : '...'; + return $prefix.implode(' ', $subtext).$suffix; + } + + protected function containsKeys($word, $keys) + { + foreach($keys as $key) + { + if(is_int(strpos($word, $key))) + return true; + } + return false; + } +} + +?> \ No newline at end of file -- cgit v1.2.3