我不想通过php创建高亮标记搜索功能
当我搜索单词的word...whole的一部分时
例如,这是一个示例文本:
英国监管机构称,交易员利用私人网络聊天室来协调他们的买卖,使货币价格对他们有利。
当我搜索"th“时,输出如下:
文字:英国监管机构表示,交易员利用私人网络聊天室协调,协调他们的买卖,用调整货币价格,让他们的更受青睐。
所以.我试过这个code...please帮我完成它。
这是一种算法:
$text= "British regulators say...";
foreach($word in $text)
{
if( IS There "th" in $word)
{
$word2= '<b>'.$word.'</b>'
replace($word with word2 and save in $text)
}
}我怎么能用php语言呢?
发布于 2014-11-12 13:42:22
function highLightWords($string,$find)
{
return preg_replace('/\b('.$find.'\w+)\b/', "<b>$1</b>", $string);
}使用:
$string="British regulators say traders used private online chatrooms to coordinate their buying and selling to shift currency prices in their favor.";
$find="th";
print_r(highLightWords($string,$find));评论后的编辑:
...How我能为中间人物做这个吗?例如“行”
非常简单,只需相应地更新regex模式即可。
return preg_replace("/\b(\w*$find\w*)\b/", "<b>$1</b>", $string); 发布于 2014-11-12 13:43:36
使用strpos()查找您搜索的字符的位置。然后开始阅读那个识别的字符位置,直到你找不到任何空间。
发布于 2014-11-12 13:40:37
应该容易得多:
$word = "th";
$text = preg_replace("/\b($word.*?)\b/", "<b>$1</b>", $text);https://stackoverflow.com/questions/26888282
复制相似问题