Jump to content
MakeWebGames

Recommended Posts

Posted

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:

string_hlight.png

Hope you find use of it ^^

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...