Jump to content

php and mysql REGEXP curly braces and max characters


fohanlon

Recommended Posts

Hi Guys

 

I am having trouble with a mysql regexp expression called through php.  I am not sure but I suspect its to do with the {} in the mysql code and PHP parsing them incorrectly.

 

Here is my code snippet:

 

$q = mysql_real_escape_string($_POST['keyword']);

$limit = 10;  //limit number of responses from dictionary

$remainder = 11 - strlen($q);

if ($q)

{

$qy = "SELECT * FROM dict_list WHERE UCASE(word) <> UCASE('$q') AND (word REGEXP '^$q.{$remainder}') LIMIT $limit";

$query = mysql_query($qy);

 

...

 

What I want to do is this:

 

a user types in a word.  I then want to query a dictionary table called dict_list for all matches of the this word up to 11 characters max.

 

Example:

 

if $q was the word aero then the response would be all words beginning with aero and up to a max of 11 characters.  That is why IU thought I could calculate length of $q and from this get the $remainder = 11 - strlen($q) then in the REGEXP use .{$remainder} but when testing if I echo out the query $qy the curly braces will not show on screen.

 

Any help would be greatly appreciated.

 

I hope this is posted in correct location. Apologies if not.

 

Thanks,

 

Fergal.

 

Link to comment
Share on other sites

It would but what about the words that are not 11 characters.  11 is the max length and I want to return any words from string length of $q up to 11 characters that begin with $q.  I can do it using LIKE but thought I could get away with using REGEXP instead.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.