我有一个搜索结果,它在截断完整字符串时严格计算 SEARCH TERM 前后的字符数。不幸的是,这会导致输出切断中间的单词。(......计数前后都有一个椭圆)
我试图让搜索结果仅在空白处与单词中间截断完整的字符串。
这是功能:
private function _highlight_results(){
$GLOBALS['_SEARCH_SUMMARY_LENGTH'] = 24;
foreach($this->results as $url => &$this_result){
if(!$this_result['url_display'] && $this_result['url']){
$this_result['url_display'] = $this_result['url'];
}
foreach($this_result['search_term'] as $search_term){
$search_term = preg_quote($search_term,'/');
foreach(array('title','summary','url_display') as $highlight_item){
if($this_result[$highlight_item] && preg_match('/'.$search_term.'/i',$this_result[$highlight_item])){
if($highlight_item != 'url_display' && strlen($this_result[$highlight_item]) > $GLOBALS['_SEARCH_SUMMARY_LENGTH']){
$boobs = ceil(($GLOBALS['_SEARCH_SUMMARY_LENGTH']-strlen($this->_search_term))/2);
preg_match('/(.{0,'.$boobs.'})('.$search_term.')(.{0,'.$boobs.'})/i',$this_result[$highlight_item],$matches);
// want to even out the strings a bit so if highlighted term is at end of string, put more characters infront.
$before_limit = $after_limit = ($boobs - 2);
if(strlen($matches[1])>=$before_limit && strlen($matches[3])>=$after_limit){
// leave limit alone.
}else if(strlen($matches[1])<$before_limit){
$after_limit += $before_limit - strlen($matches[1]);
$before_limit = strlen($matches[1]);
preg_match('/(.{0,'.($before_limit+2).'})('.$search_term.')(.{0,'.($after_limit+2).'})/i',$this_result[$highlight_item],$matches);
}else if(strlen($matches[3])<$after_limit){
$before_limit += $after_limit - strlen($matches[3]);
$after_limit = strlen($matches[3]);
preg_match('/(.{0,'.($before_limit+2).'})('.$search_term.')(.{0,'.($after_limit+2).'})/i',$this_result[$highlight_item],$matches);
}
$this_result[$highlight_item] = (strlen($matches[1])>$before_limit) ? '...'.substr($matches[1],-$before_limit) : $matches[1];
$this_result[$highlight_item] .= $matches[2];
$this_result[$highlight_item] .= (strlen($matches[3])>$after_limit) ? substr($matches[3],0,$after_limit).'...' : $matches[3];
}
}else if(strlen($this_result[$highlight_item]) > $GLOBALS['_SEARCH_SUMMARY_LENGTH']){
$this_result[$highlight_item] = substr($this_result[$highlight_item],0,$GLOBALS['_SEARCH_SUMMARY_LENGTH']).'...';
}
}
}
foreach($this_result['search_term'] as $search_term){
$search_term = preg_quote($search_term,'/');
foreach(array('title','summary','url_display') as $highlight_item){
$this_result[$highlight_item] = preg_replace('/'.$search_term.'/i','<span id="phpsearch_resultHighlight">$0</span>',$this_result[$highlight_item]);
}
}
}
}
这就是我的想法......在显示字符串输出之前,脚本应该使用“查找”椭圆和立即字符的函数循环字符串,然后在之后删除字符并继续循环直到出现空格成立。然后,下一个循环将“查找”一个字符,然后是一个椭圆,然后删除该字符并继续循环,直到在椭圆之前找到一个空格。
这是我上面描述的一些非常可悲的伪代码:
WHILE (not the end of the string) {
// NOT SURE IF I NEED A FOREACH LOOP HERE TO CHECK EACH CHAR
IF ( ^ ('...' and an immediate char are found) ) {
delete chars until a white space is found;
// if '...' is deleted along with the chars, then put the '...' back in:
//string .= '...' . string;
}
IF ( $ (a char and an immediate '...' are found) ) {
delete chars until a white space is found;
// if '...' is deleted along with the chars, then put the '...' back in:
//string .= string . '...';
}
}
PRINT string;
我认为您可以从上面的内容中了解我在寻找什么。我已经研究并测试了 wordwrap() 但仍然没有找到答案。