public function test_highlight_phrase() { $strs = array('this is a phrase' => '<strong>this is</strong> a phrase', 'this is another' => '<strong>this is</strong> another', 'Gimme a test, Sally' => 'Gimme a test, Sally', 'Or tell me what this is' => 'Or tell me what <strong>this is</strong>', '' => ''); foreach ($strs as $str => $expect) { $this->assertEquals($expect, highlight_phrase($str, 'this is')); } }
function highlight($row, $i, $filter_q) { $open_tag = '<span class="highlight-phrase">'; $close_tag = '</span>'; $str = isset($row[$this->fields[$i]]) ? $row[$this->fields[$i]] . '' : ''; $highlighted = highlight_phrase($str, $filter_q, $open_tag, $close_tag); return $this->format($highlighted, $this->fields[$i], $row, $i); }
public function _parse_single($key, $val, $string) { $newval = $val; $find = "/" . $this->l_delim . "" . $key . ".*" . $this->r_delim . "/U"; preg_match($find, $string, $matches); if (!empty($matches)) { $temp = trim($matches[0], "{}"); $res = explode(":", $temp); // var_dump($res); if (count($res) > 1) { switch ($res[1]) { case "allcaps": $newval = strtoupper($val); break; case "money": $newval = number_format((int) $val, 2, ".", ","); break; case "caps": $newval = ucwords(strtolower($val)); break; case "nocaps": $newval = strtolower($val); break; case "ucfirst": $newval = ucfirst($val); break; case "bool1": $newval = $val == 1 ? "True" : "False"; break; case "bool2": $newval = $val == 1 ? "Yes" : "No"; break; case "bool3": $newval = $val == 1 ? "Active" : "Inactive"; break; case "climit": $int = count($res) < 3 ? 128 : $res[2]; $newval = character_limiter($val, $int); break; case "htmlchars": $newval = quotes_to_entities($val); break; case "wlimit": $int = count($res) < 3 ? 25 : $res[2]; $newval = word_limiter($val, $int); break; case "wrap": $int = count($res) < 3 ? 76 : $res[2]; $newval = word_wrap($val, $int); break; case "hilite": $str = count($res) < 3 ? "" : $res[2]; $color = count($res) < 4 ? "#990000" : $res[3]; $newval = highlight_phrase($val, $str, "<span style=\"color:{$color}\">", "</span>"); break; case "safe_mailto": $alt_text = count($res) < 3 ? "" : $res[2]; $newval = safe_mailto($val, $alt_text); break; case "url_title": $sep = count($res) < 3 ? "dash" : $res[2]; $newval = url_title($val, $sep); break; case "remove_img": $newval = strip_image_tags($val); break; case "hash": $hash = count($res) < 3 ? "md5" : $res[2]; $newval = dohash($val, $hash); break; case "stripslashes": $newval = stripslashes($val); break; case "strip_tags": $allowed = count($res) < 3 ? "" : $res[2]; $newval = strip_tags($val, $allowed); break; /** other output string format options here **/ } return str_replace($matches[0], $newval, $string); } } return parent::_parse_single($key, $val, $string); }
$q = $this->db->query("select * from downloads WHERE Title LIKE '%{$query}%' order by Title"); if ($q->num_rows() == 0) { echo "<h4>Sorry, No Results Found!!</h4>"; } $results = $q->result(); foreach ($results as $rows) { ?> <li class="list-group-item"> <a href = "<?php echo $rows->Link; ?> " target = "_blank"> <?php $string = highlight_phrase($rows->Title, $query, '<span style="color:#990000">', '</span>'); echo $string; ?> </a> </li> <?php } ?> </ul> </div>
function highlight_phrases($content, $needles) { foreach ($needles as $n) { $content = highlight_phrase($content, $n); } return $content; }
</td> <?php } ?> </tr> <tr class="<?php echo $class; ?> "> <td colspan="8"><?php if ($this->session->userdata('show_desc')) { ?> <div class="clsDecrip clsAdd"> <?php $description = word_limiter($project->description, 50); echo highlight_phrase($description, $keyword, '<b>', '</b>'); ?> </div></td> </tr> <?php } } //Traverse Projects } else { echo "<tr><td colspan=8 align=center><b>No records found</b></td></tr>"; } ?> <tr> <td class="dt1 dt0" colspan="8"><table cellspacing="0" cellpadding="0" width="100%"> <tbody> <tr>
echo $this->lang->line('application_clear'); ?> </a></span></div> <div class="subcont"> <ul class="details span12"> <?php foreach ($logs as $value) { $value = str_replace("ERROR -", '', $value); ?> <?php //highlight notice $value = highlight_phrase($value, "Severity: Notice", '<span class="label label-success">', '</span>'); //highlight warning $value = highlight_phrase($value, "Severity: Warning", '<span class="label label-warning tt">', '</span>'); //highlight error $value = highlight_phrase($value, "Severity: Error", '<span class="label label-important tt">', '</span>'); if (strpos($value, 'filemtime(): stat failed for') !== false) { $value = mb_substr($value, 0, 20) . " --> Full database backup has been created."; } ?> <li style="font-size: 12px;"><?php echo $value; ?> </li> <?php } ?> <?php if (empty($logs)) { echo "<li>No log entrys yet</li>"; }
function highlight_text($text, $words) { foreach ($words as $word) { $text = highlight_phrase($text, $word, '<span style="color:#990000; font-weight: bold;">', '</span>'); } return $text; }
<div id="wrapper"> <div id="content"> <p><h4>По запросу "<?php echo $this->session->userdata('search_query'); ?> " найдено:</h4></p> <?php for ($i = $msearch_results['start_from']; $i < $msearch_results['start_from'] + $msearch_results['limit']; $i++) { if (isset($msearch_results[$i][0])) { $msearch_results[$i][1] = highlight_phrase($msearch_results[$i][1], $this->session->userdata('search_query'), '<span style="background:#FFFF66">', '</span>'); $msearch_results[$i][2] = highlight_phrase($msearch_results[$i][2], $this->session->userdata('search_query'), '<span style="background:#FFFF66">', '</span>'); print <<<HERE <table> <tr> <td align = "center"> <p><a href = "http://abclass.ru/materials/{$msearch_results[$i][0]}"><img class="small_img" src="{$msearch_results[$i][3]}" width="45" height="45"></a></p> </td> <td align = "center"> <p class = "anons_title"><a href = "http://abclass.ru/materials/{$msearch_results[$i][0]}">{$msearch_results[$i][1]}</a></p> <p class = "anons_text">Добавил: {$msearch_results[$i][4]}<br> Просмотров материала: {$msearch_results[$i][5]}</p> </td> </tr> </table> <p>{$msearch_results[$i][2]}</p>
public function index() { //搜索关键词 if ($this->input->get('query')) { $query = mb_substr(merge_spaces(trim($this->input->get('query'))), 0, 30); $search['query'] = $query; } else { $search['query'] = ''; $query = ''; } $data['query'] = $query; //搜索的类型and/or if ($this->input->get('type')) { $search['type'] = $this->input->get('type'); } else { $search['type'] = 'or'; } //url是否把链接加入搜索权重 if ($this->input->get('url')) { $search['url'] = $this->input->get('url'); } else { $search['url'] = 'all'; } //每页显示多少条结果 if ($this->input->get('results')) { $search['results'] = $this->input->get('results'); } else { $search['results'] = '0'; } $quantity_view = $this->base_setting->get_setting('quantity_view'); //每页显示数 $search['quantity_view'] = $quantity_view; $spider_all = $this->search_model->get_spider_like($search); //搜索关键词 //处理高亮显示,把字符串转成单字数组 /* $q_arr = SBC_DBC($query,1);//全角转半角 $q_arr = preg_replace("/\s/","",$q_arr);//去空格 $q_arr = split_string_to_array($q_arr); */ $q_arr = explode(' ', $query); $q_arr_count = count($q_arr); //处理高亮显示,把字符串转成单字数组 //块布局左 $this->load->module('common/module_left'); $data['module_left'] = $this->module_left->index(); //块布局右 $this->load->module('common/module_right'); $data['module_right'] = $this->module_right->index(); //底部 $this->load->module('common/module_bottom'); $data['module_bottom'] = $this->module_bottom->index(); //设定span9的css样式 if (!empty($data['module_right']) && empty($data['module_left'])) { $header['style'] = ' <style type="text/css"> @media(min-width: 980px){ .search .row-fluid .span9{ padding-right: 150px; } } </style> '; } //------------------------------------------------ $header['title'] = $query; $header['css_page_style'] = array('public/css/blog.css', 'public/css/jquery.fancybox.css', 'public/css/jquery.gritter.css'); $this->public_section->get_header($header); $this->public_section->get_top(); //遍历处理结果数组 $data['results'] = array(); foreach ($spider_all['content'] as $s_k => $s_v) { //权重 $spider[$s_k]['weight'] = ''; //链接 $spider[$s_k]['url'] = $spider_all['content'][$s_k]['url']; //计算权重,统计字符长度 $title_strlen = strlen($spider_all['content'][$s_k]['title']); $content_strlen = strlen($spider_all['content'][$s_k]['content']); //计算权重排序,计算相似度 if (!empty($query) && is_array($q_arr)) { foreach ($q_arr as $q_k => $q_v) { //关键字长度/标题长度*相似度 //标题 @($spider[$s_k]['weight']['title'][$q_k] = strlen($q_arr[$q_k]) / $title_strlen * 0.2 + $this->similarity->getSimilar($q_arr[$q_k], $spider_all['content'][$s_k]['title']) * 0.4); //内容 $spider[$s_k]['weight']['content'][$q_k] = strlen($q_arr[$q_k]) / $content_strlen * 0.12 + $this->similarity->getSimilar($q_arr[$q_k], $spider_all['content'][$s_k]['content']) * 0.28; } //计算元素的和 $spider[$s_k]['weight'] = array_sum($spider[$s_k]['weight']['title']) + array_sum($spider[$s_k]['weight']['content']); } //循环关键字,高亮 //标题 $title = mb_substr($spider_all['content'][$s_k]['title'], 0, 18, 'utf-8'); //正文 $content = mb_substr($spider_all['content'][$s_k]['content'], 0, 100, 'utf-8'); for ($i = 0; $i < $q_arr_count; $i++) { //高亮显示文本字符串 $title = highlight_phrase($title, $q_arr[$i]); $content = highlight_phrase($content, $q_arr[$i]); } $spider[$s_k]['title'] = $this->public_section->word_censor($title); //标题 $spider[$s_k]['content'] = $this->public_section->word_censor($content); //正文 } //按权重分排序 if (isset($spider)) { foreach ($spider as $weight_) { $weight_order[] = $weight_['weight']; } array_multisort($weight_order, SORT_DESC, $spider); } //arsort($spider); @($data['results'] = $spider); //分页 //链接 $url = ''; $url .= 'type=' . $search['type']; $url .= '&url=' . $search['url']; $url .= '&query=' . $query; $config['full_tag_open'] = '<ul>'; $config['full_tag_close'] = '</ul>'; $config['first_link'] = '首页'; $config['last_link'] = '尾页'; $config['first_tag_open'] = '<li>'; $config['next_tag_open'] = '<li>'; $config['prev_tag_open'] = '<li>'; $config['last_tag_open'] = '<li>'; $config['cur_tag_open'] = '<li class="active"><a>'; $config['num_tag_open'] = '<li>'; $config['first_tag_close'] = '</li>'; $config['next_tag_close'] = '</li>'; $config['last_tag_close'] = '</li>'; $config['prev_tag_close'] = '</li>'; $config['cur_tag_close'] = '</a></li>'; $config['num_tag_close'] = '</li>'; $config['next_link'] = '下一页'; $config['prev_link'] = '上一页'; $config['base_url'] = site_url('search?' . $url); $config['total_rows'] = $spider_all['count']; $config['per_page'] = $quantity_view; //每页显示条数 $config['page_query_string'] = TRUE; $config['query_string_segment'] = 'results'; if ($this->agent->is_mobile()) { $config1['display_pages'] = FALSE; } else { $config1['num_links'] = '2'; } $this->pagination->initialize($config); $data['count'] = $spider_all['count']; $data['search_page'] = $this->pagination->create_links(); $this->load->view('tools/search', $data); $this->public_section->get_footer(); }
<b><?php echo $keyword; ?> </b></span></h3> <!--FAQ ANSWER--> <div id="selFAQ" class="clsMarginTop"> <ul> <?php if (isset($faqs) and $faqs->num_rows() > 0) { foreach ($faqs->result() as $faq) { ?> <li><a href="<?php echo site_url('faq/view/' . $faq->id); ?> "><?php echo highlight_phrase($faq->question, $keyword, '<b>', '</b>'); ?> </a></li> <?php } } else { echo $this->lang->line('No records found'); } ?> </ul> </div> <!--END OF FAQ ANSWER--> </div> </div> </div> </div>
public function text_helper() { $this->load->helper('text'); $string = "Here is a nice text string consisting of eleven words."; $string = word_limiter($string, 4, "..."); $this->htmlp($string); $string = "Here is a nice text string consisting of eleven words."; $string = character_limiter($string, 20, "..."); $this->htmlp($string); $string = ascii_to_entities($string); $this->htmlp($string); $string = convert_accented_characters($string); $this->htmlp($string); $string = "darn shit sucks dinner"; $disallowed = array('darn', 'shucks', 'golly', 'phooey'); $string = word_censor($string, $disallowed, 'Beep!'); $this->htmlp($string); $string = highlight_code($string); $this->htmlp($string); $string = "Here is a nice text string about nothing in particular."; $this->htmlp(highlight_phrase($string, "nice text", '<span style="color:#990000;">', '</span>')); $string = "Here is a simple string of text that will help us demonstrate this function."; $this->htmlp(word_wrap($string, 25)); $str = 'this_string_is_entirely_too_long_and_might_break_my_design.jpg'; $this->htmlp(ellipsize($str, 32, 0.5)); }
} ?> <tr item_id="<?php echo $row[$keys[0]]; ?> " class="<?php echo $rowClass; ?> "> <td style="display: none;">hidden</td> <td class="text-center"><?php echo $index + 1; ?> </td> <td class="text-center"><?php echo highlight_phrase($row[$key], $search_text, '<span style="font-weight:bold;background-color:yellow;">', '</span>'); ?> </td> </tr> <?php } ?> </tbody> </table> <?php } } else { ?> <h4> No Matches Found.</h4> <?php }
foreach ($search_list as $v) { ?> <li class="media"> <div class="pull-right"> <span class="badge badge-info topic-comment"><?php echo $v['comments']; ?> </span> </div> <div class="media-body"> <h4 class="media-heading"><a href="<?php echo url('topic_show', $v['topic_id']); ?> "><?php echo highlight_phrase($v['title'], $keyword, '<span class=red>', '</span>'); ?> </a> <span class="text-muted"><?php echo friendly_date($v['updatetime']); ?> </span></h4> </div> </li> <?php } ?> </ul> <?php if ($pagination) { ?>
<?php if (!empty($posts)) { ?> <?php foreach ($posts as $post) { ?> <h2><a href="<?php echo $post->url; ?> "><?php echo highlight_phrase($post->title, $q, '<span class="search_highlight">', '</span>'); ?> </a></h2> <?php echo highlight_phrase($post->get_excerpt_formatted(50, '', TRUE), $q, '<span class="search_highlight">', '</span>'); ?> <?php } ?> <?php } else { ?> <p>There were no search results returned.</p> <?php } ?> <?php } else {
function dq_highlight_phrase($str, $phrases = array(), $start = '<strong class="searchedWord">', $end = '</strong>') { foreach ($phrases as $keyword) { $str = highlight_phrase($str, $keyword, $start, $end); } return $str; }
<td><div align="center"><input type="checkbox" class="checkinput" value="<?php echo $row->id; ?> " name="checkinput[]" ></div></td> <td widtd="68%"><div align="center"><a href="<?php echo $this->admin_url . 'cnews/edit/' . $row->newscatalogue_id . '/' . $row->id; ?> "><img src="<?php echo $row->dir . ($row->old_id == 1 ? $row->image : filenameplus($row->image, 'medium')); ?> " width="100" height="100" /></a></div></td> <td widtd="68%"><a href="<?php echo $this->admin_url . 'cnews/edit/' . $row->newscatalogue_id . '/' . $row->id; ?> "><?php echo $string = highlight_phrase($row->title_vietnamese, $searchkey, '<span class="highlighttext">', '</span>'); ?> <span style="color:#9C3">(<?php echo get_from_datetime($row->created); ?> )</span></a></td> <td widtd="68%"><div align="left"> <?php if ($row->hot == 1) { ?> <img src="<?php echo $admin_resource; ?> new/images/hot.jpg"/> <?php } ?>
$ct3 = "..." . word_limiter($ct2, 52); } else { $ct3 = $ct; /* Hàm search này không hiển thị được highlight đối với chữ tiếng Việt (nhưng vẫn ra kết quả search đúng */ /*Nếu như độ dài bài viết nhỏ hơn 52 thì có thể hiển thị cả bài viết, do đó mà từ khóa cần tìm chắc chắn sẽ được bôi đậm trong bài, ko cần cắt bài nữa. */ } //echo $rows['description']; echo highlight_phrase($ct3, $key, '<span style="color:#990000">', '</span>'); ?> </div> </div> </div> <?php } ?> </div> </div> <!--END NOI DUNG--> </div> <!--END CONTENT--> </div>