NYCPHP Meetup

NYPHP.org

[nycphp-talk] Highlighting keywords in query

Analysis & Solutions danielc at analysisandsolutions.com
Sat May 17 13:35:04 EDT 2003


Hi Dennis:

On Sat, May 17, 2003 at 11:30:56AM -0400, Dennis Butler wrote:
> If I wanted to print just the keywords the user typed into a form, 
> within the query results, say in a red font, but leave the rest of the 
> data from the table cell in black, is there a way of inserting some 
> logic inside the print statement within an SQL query to isolate only the 
> matched word that was typed in.

There are probably existing tutorials out there about this.  I haven't
done this personally.  But, I can think of two ways to do it.

One is in the SQL query.  For example, MySQL has a REPLACE() function.

The other is a preg_replace() the resulting text before you output it.  
I'd probably go this way.  Get your search terms into a string.  There are 
lots of ways to do this, depending on how your searh terms are initially 
acquired/defined, but here's one:

   $Terms = implode('|', $SearchTerms);

Then put the terms into a regex:

   $out = preg_replace("/($Terms)/i", 
                       '<b class="term">\\\\1</b>', $out);

Then use style sheets to make the "color" of the class "term" red.  This 
has better cross platform performance than <font> and uses less space:

   <style type="text/css">
      b.term {color: red}
   </style>

Note: the above code is off the top of my head and hasn't been tested.

Enjoy,

--Dan

-- 
     FREE scripts that make web and database programming easier
           http://www.analysisandsolutions.com/software/
 T H E   A N A L Y S I S   A N D   S O L U T I O N S   C O M P A N Y
 4015 7th Ave #4AJ, Brooklyn NY    v: 718-854-0335   f: 718-854-0409



More information about the talk mailing list