2012-04-25 80 views
1

我有一个搜索结果,当切断整个字符串时,它严格计算SEARCH TERM前后的字符数。不幸的是,这会导致输出中断单词。 (...在计数之前和之后有一个椭圆)PHP - 在不切断单词的情况下显示输出

我想让搜索结果仅在白色空间与一个字中间切断全字符串。

下面是函数:

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]); 
      } 
     } 
    } 
} 

这里是我的脚本应该通过串循环使用功能“查找”椭圆和思考...... 只显示字符串输出之前即时字符,然后删除字符AFTER并继续循环,直到找到空白区域。然后,下一个循环将'查找'一个字符,然后是一个椭圆,然后删除该字符并继续循环,直到在椭圆之前找到一个空白空间。

这里是我上面描述的一些很伤心伪代码:

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(),但仍未找到答案。

+0

我建议你在本网站上使用正则表达式搜索wordwrap,它应该给你一些指针,因为它是相似的。 – hakre 2012-04-25 08:52:40

+0

试试这个链接,可以帮助你... http://stackoverflow.com/a/26098951/3944217 – edCoder 2014-09-29 12:08:49

回答

0

下面是一个应该可以正常工作并且性能很好的方法。唯一的缺点是,它只能在空格上打破单词,并且这不能被简单地修复,因为没有strrspn函数来补充strspn(但可以很容易地编写并用于扩展此解决方案)。

function display_short($str, $limit, $ellipsis = '...') { 
    // if all of it fits there's nothing to do 
    if (strlen($str) <= $limit) { 
     return $str; 
    } 

    // $ellipsis will count towards $limit 
    $limit -= strlen($ellipsis); 

    // find the last space ("word boundary") 
    $pos = strrpos($str, ' ', $limit - strlen($str)); 

    // if none found, prefer breaking into the middle of 
    // "the" word instead of just giving up 
    if ($pos === false) { 
     $pos = $limit; 
    } 

    return substr($str, 0, $pos).$ellipsis; 
} 

测试用:

$string = "the quick brown fox jumps over the lazy dog"; 
for($limit = 10; $limit <= strlen($string); $limit += 10) { 
    print_r(display_short($string, $limit)); 
} 

See it in action

+0

我试过这个功能,但收到此错误:调用未定义的函数display_short()...任何想法? – mar2195 2012-04-25 19:17:01

+0

在这一点上,我认为处理这个最简单的方法就是问别人一个正则表达式是这样的: FOREACH CHAR { \t的preg_replace( 搜索从开始STRING -then- 找到“...” - 则─ IF CHAR不是白色空间,请删除CHAR(或继续寻找空白?)-then- IF CHAR是白色的空间,BREAK ) } ,然后又preg_replace函数为结束” ... ' – mar2195 2012-04-25 19:42:29

+0

@ user1355539:我认为最简单的方法是将Ideone上的实例与您的代码进行比较,并查看出错的位置。它不是那么难。 :) – Jon 2012-04-25 22:18:44

相关问题