PioneerMan
PioneerMan

Reputation: 323

How do I use PHP string interpolation and square brackets in a MySQL regexp?

In my query I have:

$search .= "and title REGEXP '[[:<:]]$q[[:>:]]' ";

Which produces the following error in the Apache error log:

PHP Parse error:  syntax error, unexpected '[', expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)

I tried putting {} around it, but that didn't work either.

Thank you.

Upvotes: 0

Views: 333

Answers (1)

Rick James
Rick James

Reputation: 142518

Add braces: $search .= "and title REGEXP '[[:<:]]{$q}[[:>:]]'"; -- otherwise it thinks you are referencing the $q array.

PHP allows array lookups in strings, such as "... $q[...] ..." It also allows expresssions: "... {$a+$b} ..." if you add the braces. Putting those together solves this problem.

Upvotes: 2

Related Questions