views:

37

answers:

3

How can I match a word in a sentence from a column using regex? I tried:

"select * from table where column regex '/\b".$text."\b/i'"

but that didn't work. Thanks in advance.

+1  A: 

MySQL is particularly bad about stuff like this. I'd recommend using MATCH AGAINST syntax http://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html or using something like SOLR if you're going to be doing this in volume.

UltimateBrent
+1  A: 

Try this:

"SELECT * FROM table WHERE column REGEXP '[[:<:]]" . $text . "[[:>:]]'"

You should make sure that any characters that could be interpreted as special characters in $text are properly escaped. You should also ensure that you do not get an SQL injection.

Mark Byers
A: 

I think you want the LIKE clause!

SELECT * FROM table WHERE column LIKE '%value%'

Also please read UltimateBrent's answer as full text search is a very good point.

RobertPitt
LIKE can't detect word boundaries.
Mark Byers
I know this.. that is why I also advised him to check your answer.
RobertPitt