11
08月
2015
32. 搜索和高亮字符串中的关键字
1
2
3
4
5
6
7
8
9
10
11
|
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 ;
}
|
语法:
1
2
3
4
5
|
<?php
$string = "I like chocolates and I like apples" ;
$words = "apple" ;
echo highlighter_text( $string , $words );
?>
|
特殊说明,本文版权归 ning个人博客 所有带原创标签请勿转载,转载请注明出处.