dongqiao1151 2011-10-31 09:59
浏览 37
已采纳

在文本中搜索关键字之前截断内容

I am using the below code to truncate my content before and after the first search keyword in my text (this is for my search page) everything works as it should apart from the code cutting words in half at the beginning of the truncate, it doesn't cut words at the end of the truncate.

Example:

lients at the centre of the relationship and to offer a first class service to them, which includes tax planning, investment management and estate planning. We believe that our customer focused and...

(edit:it is sometimes more than one character missing from the word)

You will see that it has chopped the 'c' off 'clients'. It only happens at the beginning of the text not the end. How can I fix this? I believe I am half way there. code so far:

function neatest_trim($content, $chars, $searchquery,$characters_before,$characters_after) {
            if (strlen($content) > $chars) {
                 $pos = strpos($content, $searchquery);
                 $start = $characters_before < $pos ? $pos - $characters_before : 0;
                $len = $pos + strlen($searchquery) + $characters_after - $start;
                $content = str_replace('&nbsp;', ' ', $content);
                $content = str_replace("
", '', $content);
                $content = strip_tags(trim($content));
                $content = preg_replace('/\s+?(\S+)?$/', '', mb_substr($content, $start, $len));
                $content = trim($content) . '...';
                $content = strip_tags($content);
                $content = str_ireplace($searchquery, '<span class="highlight" style="background: #E6E6E6;">' . $searchquery . '</span>', $content);
            }
            return $content;
        }



 $results[] = Array(
  'text' => neatest_trim($row->content,200,$searchquery,120,80)
            );
  • 写回答

2条回答 默认 最新

  • doulang6013 2011-10-31 10:21
    关注

    The 120 Characters that you are keeping at the start don't check if the 120th character is a space or a letter, and just cuts the string there no matter what.

    I would make this change, to search for the closest "space" to the position we are starting from.

    $start = $characters_before < $pos ? $pos - $characters_before : 0;
    // add this line:
    $start = strpos($content, ' ', $start);
    $len = $pos + strlen($searchquery) + $characters_after - $start;
    

    This way $start is the position of a space, and not a letter from a word.

    Your Function would become:

    function neatest_trim($content, $chars, $searchquery,$characters_before,$characters_after) {
        if (strlen($content) > $chars) {
        $pos = strpos($content, $searchquery);
        $start = $characters_before < $pos ? $pos - $characters_before : 0;
        $start = strpos($content, " ", $start);
        $len = $pos + strlen($searchquery) + $characters_after - $start;
        $content = str_replace('&nbsp;', ' ', $content);
        $content = str_replace("
    ", '', $content);
        $content = strip_tags(trim($content));
        $content = preg_replace('/\s+?(\S+)?$/', '', mb_substr($content, $start, $len));
        $content = trim($content) . '...';
        $content = strip_tags($content);
        $content = str_ireplace($searchquery, '<span class="highlight" style="background: #E6E6E6;">' . $searchquery . '</span>', $content);
        }
        return $content;
      }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码