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

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -