NYCPHP Meetup

NYPHP.org

[nycphp-talk] PHP STRING QUESTION

Ophir Prusak prusak at gmail.com
Tue Oct 19 15:41:20 EDT 2004


Just curious, why are you exploding the string into an array?
You can just do:

    function highlight($keyword, $text) {
                 $text = preg_replace('/\b' . $keyword .
'\b/i','<b>\\0</b>',$text);
                 return $text;
    }


On Tue, 19 Oct 2004 14:59:52 -0400, Mark Armendariz <nyphp at enobrev.com> wrote:
> > 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
> 
> 
> 
> _______________________________________________
> New York PHP Talk
> Supporting AMP Technology (Apache/MySQL/PHP)
> http://lists.nyphp.org/mailman/listinfo/talk
> http://www.newyorkphp.org
>



More information about the talk mailing list