php - search in words on an string and change formatting -


i wan't create highlight tag search function via php

when search part of word...whole of word colored

for example sample text:

text: british regulators traders used private online chatrooms coordinate buying , selling shift currency prices in favor.

when search "th" output this:

text: british regulators traders used private online chatrooms coordinate their buying , selling shift currency prices in their favor.

so...i tried code...please me complete it.

this algorithm:

$text= "british regulators say..."; foreach($word in $text) {   if( there "th" in $word)    {       $word2= '<b>'.$word.'</b>'       replace($word word2 , save in $text)     } } 

how can in php language?

function highlightwords($string,$find) {    return preg_replace('/\b('.$find.'\w+)\b/', "<b>$1</b>", $string);  } 

usage:

$string="british regulators traders used private online chatrooms coordinate buying , selling shift currency prices in favor."; $find="th"; print_r(highlightwords($string,$find)); 

fiddle

edit after comment:

...how can middle characters? example "line"

very easy, update regex pattern accordingly

return preg_replace("/\b(\w*$find\w*)\b/", "<b>$1</b>", $string);  

fiddle


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -