LordDan Posted July 26, 2010 Share Posted July 26, 2010 Hey! Been away from PHP for a few weeks and cannot bare it any longer so I had to type something! Anyway, I decided to write a quick Search Phrase Highlighter. PHP: function highlight_words( $string, $words, $color = '#FFFF99' ){ $words = ( is_array( $words ) ) ? $words = implode( '|', $words ) : $words; return preg_replace( '@\b('. $words .')\b@si', '<span style="background-color:'. $color .'">$1</span>', $string ); } Usage: echo highlight_words( 'This is my string ya\'know!', 'my' ) .' '; echo highlight_words( 'This is my string ya\'know!', array('my', 'this'), '#99FFCC' ); Result: Hope you find use of it ^^ Quote Link to comment Share on other sites More sharing options...
Equinox Posted July 26, 2010 Share Posted July 26, 2010 This could be quite useful actually, I like it. Make a search feature and it highlights the words the user input. Quote Link to comment Share on other sites More sharing options...
Danny696 Posted July 26, 2010 Share Posted July 26, 2010 This would be usefull. I may use this in an upgrade of my blog system. Quote Link to comment Share on other sites More sharing options...
Djkanna Posted July 26, 2010 Share Posted July 26, 2010 Nice one, mind if I add this to my collection of resources? :P Quote Link to comment Share on other sites More sharing options...
LordDan Posted July 26, 2010 Author Share Posted July 26, 2010 Use at will ^^ Thanks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.