Hi all
Can somebody point me in right direction on how to highlight search result (whole word).
Example
$search = "highlight sear";
$recordset = "I would like to highlight some words from search resoults";
Thank you
regards ddragas
highlight search result
Moderator: General Moderators
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
preg_replace() is one.
Code: Select all
if(!empty($_GET['highlight']))
{
if(preg_match_all('@[^\W]{3,}@', $_GET['highlight'], $matches))
{
$content = preg_replace(
'~\b('.implode('|', $matches[0]).')\w*\b(?![^<]*[>])~is',
'<span class="highlight">$0</span>',
$content
);
}
}The code above does that but may lead to unpredictible results like cat highlighting category which may or may not be what you wanted. To stop this behaviour remove the "\w*" from the expession.ddragas wrote:Can somebody point me in right direction on how to highlight search result (whole word).
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
It does work as can be seen here: http://prueba.revistafoto.es/concurso-f ... 1150322401 The only difference is the name of the variable, highlight >> destacar.ole wrote:If bokehman's code is good it should surely be added to code snippets.
I haven't tested it but it sure impressed me, nice work bokehman.