arrays - PHP using preg_replace to highlight string that is part of a word -


so have following code highlights word/s entered search bar in search results:

$sk = explode(" ",$searchval);      foreach ($searchpostresults $pageposts)         echo '<li><span class="searchresult"><a href="'          . get_the_permalink($pageposts->id) . '" title="">' .          preg_replace('/\b(' . implode('|', $sk) . ')\b/iu',                       '<strong class="searchhighlight">\0</strong>',                       get_the_title($pageposts->id))           . '</a></span></li>'; 

now works part. lets enter search term "how to" in search bar, word how highlighted. if word how inside word shower, highlight how so:

s<strong class="searchhighlight">how</strong>er 

anyone know how might adjust code this.

cheers

the behavior describing opposite of peoples intentions, , why regex has \b in it. \b word boundary , makes can have full word matches. remove \bs , should work. won't need capture group.

preg_replace('/' . implode('|', $sk) . '/iu', 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -