LordDan Posted July 26, 2010 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
Equinox Posted July 26, 2010 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
Danny696 Posted July 26, 2010 Posted July 26, 2010 This would be usefull. I may use this in an upgrade of my blog system. Quote
Djkanna Posted July 26, 2010 Posted July 26, 2010 Nice one, mind if I add this to my collection of resources? :P Quote
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.