PHP, how can I produce a string, a unique list of values up to three items, for use after IN in a query?

Posted by Jules on Stack Overflow See other posts from Stack Overflow or by Jules
Published on 2011-03-14T08:08:52Z Indexed on 2011/03/14 8:09 UTC
Read the original article Hit count: 158

Filed under:

I need to produce a string for use in an query e.g.

SELECT whatever from Keywords.word IN (here);

At the moment I have string which could be

$search = "one word or four";

or

$search = "one";

or

$search = "one one";

I need to validate this into some acceptable for my query. I want a unique list of words, separated with a comma up to a maximum of three.

This is what I have so far.

$array = explode(" ",$search);
$unique = array_unique ($array);

I'm sure there must be a quicker way than evaluating each of the items for blank and selecting the first three.

© Stack Overflow or respective owner

Related posts about php