KBD

Keith Devens .com

Friday, January 9, 2009 Flag waving
The three chief virtues of a programmer are: Laziness, Impatience and Hubris. – Larry Wall (Programming Perl)
← It's not about oilThe tyranny of "BUT" →

Daily link icon Friday, January 17, 2003

One-liner of the day

Just wanted to share my PHP "one-liner" for today. I finally got fed up at my weblog search feature for not searching post titles, so I wrote the code to do it. This line appears in some code that's dynamically building the SQL statement to send to MySQL...

<?php
    $search_param 
join(' AND 'array_map(create_function('$a''return "(Entry_Title LIKE \'%$a%\' OR Entry_Text LIKE \'%$a%\')";'), $search_words));
?>

That's the logic that produces the results for a search if "Phrase search" is unchecked. So you see, each word you search for has to appear in either in the post title or the post text. In other words, it's an AND search not an OR search.

This also works for the degenerate case where you do a phrase search and everything you enter is treated as a string (like a single "search word").

← It's not about oilThe tyranny of "BUT" →

Comments XML gif


Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

January 2009
SunMonTueWedThuFriSat
 123
45678910
11121314151617
18192021222324
25262728293031



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 3 posts

Recent comments XML

The Elegant Universe

Well I have finally found the crazy​guy that preaches useless nonsence​in A...

Joseph Baxter: Jan 7, 11:07pm

I hate Norton Antivirus


SYMANTEC is very​cunning..
Symantec now have a​redeemable cash back offe...

CAN: Jan 4, 6:25pm

Spider solitaire

Hi everyone!

Glad to have found​this site.  I have enjoyed reading​the c...

flwrchld53: Jan 4, 5:30pm

Generated in about 0.179s.

(Used 8 db queries)

mobile phone