php - highlighting all keywords in one string -
i have created code follows:
$replace = preg_replace("/($a)/","<b>$a</b>",$search); $output .= '<div>'.$replace.'</div>'; print("$output")
- $a = keywords matching entries in database
- $search = phrase entered searching
the result phrase highlighting matched keywords shown div boxes independently. here example (assuming "abcde" phrase search, , "ab" , "e" matched keywords):
- abcde.
- abcde.
is possible highlight matched keywords in string within 1 div box? this:
- abcde.
if want make replacement few elements, use preg_replace_callback :
function makebold($match) { return "<b>" . $match[0] . "</b>"; } $pattern = "/[abe]/"; $str = "abcde"; $output = preg_replace_callback($pattern, "makebold", $str); echo $output;
Comments
Post a Comment