dongshang1529 2011-10-30 11:26
浏览 14
已采纳

谷歌喜欢搜索结果

Leading on from my last question:

Tidying search results from database php

My research has led me to the following code to truncate search results, highlight the keyword and display a number of characters to the left and right of the keyword. Looking better.

My question now is, with the below code, by adding characters to the left or right of the keyword it is cutting words in half.

example:

Savings plans

..le="background: #E13300;">investing in international accounts is no longer the premise of the rich and famous, all expatriates living abroad can now enjoy flex...

How can I make the truncate function stop cutting my words and tags in half????

Code:

 $word = 'invest'; 
$characters_before="80";
$characters_after="80";
function supertruncate($text, $word, $characters_before, $characters_after){
                  $pos = strpos($text, $word);
    $start = $characters_before < $pos ? $pos - $characters_before : 0;
    $len = $pos + strlen($word) + $characters_after - $start;
                   $text = str_ireplace($word, '<span class="highlight" style="background: #E13300;">' . $word . '</span>', $text);
    return substr($text, $start, $len);
}
  • 写回答

1条回答 默认 最新

  • duanchong3075 2011-10-30 11:30
    关注

    Truncate first. Then highlight.

    $text = substr($text, $start, $len);
    return str_ireplace($word, '<span class="highlight" style="background: #E13300;">' . $word . '</span>', $text);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!