PHP搜索和高亮字符串中的关键字

2015-08-09 0 304
PHP搜索和高亮字符串中的关键字
function highlighter_text($text, $words)
{
    $split_words = explode( " " , $words );
    foreach($split_words as $word)
    {
        $color = "#4285F4";
        $text = preg_replace("|($word)|Ui" ,
            "<span style=\"color:".$color.";\"><b>$1</b></span>" , $text );
    }
    return $text;
}

用法:

<?php
$string = "I like chocolates and I like apples";
$words = "apple";
echo highlighter_text($string ,$words);
?>

遇见资源网 php PHP搜索和高亮字符串中的关键字 http://www.ox520.com/7963.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务