Google 喜欢的搜索结果
从我的上一个问题开始:
我的研究有引导我使用以下代码来截断搜索结果,突出显示关键字并在关键字的左侧和右侧显示一些字符。看起来更好了。
我现在的问题是,使用下面的代码,通过在关键字的左侧或右侧添加字符,可以将单词减半。
示例:
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...
如何让截断函数停止将我的单词和标签切成两半???
代码:
$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);
}
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
先截断。然后突出显示。
Truncate first. Then highlight.