Jump to content


Photo

Using REGEXP with SELECT staements matching whole words within a field


  • Please log in to reply
3 replies to this topic

#1 jmichael68

jmichael68
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 06 October 2006 - 04:08 PM

Here is the code that I have problems with...

$query22 = "SELECT '$text' from dpl WHERE $field REGEXP '[[:<:]]$text[[:>:]]'";

I'm looking to match whole words that are in variable $text.  This works when you put '[[:<:]]JOHN[[:>:]]',

is there a way to use this statement with a variable???

#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 06 October 2006 - 04:14 PM

This should work. Make sure $text and $field contain what you expect, and that the database connection and selection is working properly.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 jmichael68

jmichael68
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 06 October 2006 - 06:12 PM

if I use this:


$query22 = "SELECT * from dpl WHERE Name REGEXP '[[:<:]]$name[[:>:]]'";

I get this error message:

Parse error: parse error, unexpected '[', expecting T_STRING or T_VARIABLE or T_NUM_STRING

but my array is returning this....
SELECT * from dpl WHERE Name REGEXP '[[:<:]]RAY[[:>:]]'
SELECT * from dpl WHERE Name REGEXP '[[:<:]]VLADIMIR[[:>:]]'
SELECT * from dpl WHERE Name REGEXP '[[:<:]]MOHAMMED[[:>:]]'

when I hard code "RAY" in the statement like this...
SELECT * from dpl WHERE Name REGEXP '[[:<:]]RAY[[:>:]]'

It works???

#4 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 06 October 2006 - 06:41 PM

Ah... $name[ is being seen as an array, change it to $name\[.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users