PHP - 显示输出而不会切断单词

时间:2012-04-25 08:39:22

标签: php split word word-wrap

我有一个搜索结果,在切断完整字符串时严格计算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]);
            }
        }
    }
}

这就是我的想法...... 在显示字符串输出之前,脚本应使用“查找”椭圆和直接字符的函数循环遍历字符串,然后删除字符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()但仍未找到答案。

1 个答案:

答案 0 :(得分: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