NYCPHP Meetup

NYPHP.org

[nycphp-talk] PHP STRING QUESTION

Mark Armendariz nyphp at enobrev.com
Tue Oct 19 14:59:52 EDT 2004


> I am able to search the word using the stristr() 
> function in php. This function is non-case sensitive. 
> However, I want to display the searched word in the line as highlighted in
a 
> different color than the line.

I wasn't sure if you wanted to return a line from the text (which Dan seems
to have taken care of pretty well) or if you just wanted to replace words
with formatted versions of themselves.  Here's a really basic function I use
to do the latter with multiple keywords (sep. by spaces):

<?php
    function highlight($keywords, $text) {
        $keywords = trim($keywords);
        
        if (strlen($keywords)) {
            $keywords = explode(' ', $keywords);
            
            if (count($keywords)) {
                foreach($keywords as $keyword) {
		        // you could, of course use whatever html formatting
you want here
                    $text = preg_replace('/(' . $keyword . ')/i',
'<b>\\1</b>', $text); 
                }
            }
        }
            
        return $text;
    }
?>

Good Luck!

Mark




More information about the talk mailing list