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") 
  1. $a = keywords matching entries in database
  2. $search = phrase entered searching

the result phrase highlighting matched keywords shown div boxes independently. here example (assuming "abcde" phrase search, , "ab" , "e" matched keywords):

  1. abcde.
  2. abcde.

is possible highlight matched keywords in string within 1 div box? this:

  1. 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

Popular posts from this blog

symfony - TEST environment only: The database schema is not in sync with the current mapping file -

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -