站内搜索,返回的结果怎么高亮显示关键字?
站内搜索,返回的结果怎么高亮显示关键字?
可以使用成对替换函数strtr:
string strtr ( string $str , array $replace_pairs )
<code><?php
header('Content-Type: text/plain; charset=utf-8');
$arr = array('中国', '中国人'); //关键字
foreach($arr as $v) { $new[$v] = '<b>'.$v.'</b>'; }
var_export($new); //输出: array( '中国' => '<b>中国</b>', '中国人' => '<b>中国人</b>' )
$str = '我是中国人我爱中国';
echo strtr($str, $new)."\n"; //输出: 我是<b>中国人</b>我爱<b>中国</b>
//对比:str_replace会发生重复替换,下面代码会输出: 我是<b><b>中国</b>人</b>我爱<b>中国</b>
echo str_replace(array('中国人','中国'), array('<b>中国人</b>','<b>中国</b>'), '我是中国人我爱中国');</code>
建议使用 elasticsearch 这个是java写的搜索引擎,基于lunce
<code><?php $str = "Hello world!"; $keyword = "world"; echo str_replace($keyword, "<font color='red'>" . $keyword . "</font>", $str); ?></code>
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号