fohanlon Posted March 3, 2011 Share Posted March 3, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/229476-php-and-mysql-regexp-curly-braces-and-max-characters/ Share on other sites More sharing options...
Muddy_Funster Posted March 3, 2011 Share Posted March 3, 2011 wouldn't it be much easier to just make the query do it? $qry = "SELECT word, definition FROM dict_list WHERE UCASE(word) LIKE UCASE('$q%') AND CHAR_LENGTH(word) = 11 LIMIT $limit"; Quote Link to comment https://forums.phpfreaks.com/topic/229476-php-and-mysql-regexp-curly-braces-and-max-characters/#findComment-1182308 Share on other sites More sharing options...
fohanlon Posted March 3, 2011 Author Share Posted March 3, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/229476-php-and-mysql-regexp-curly-braces-and-max-characters/#findComment-1182309 Share on other sites More sharing options...
Muddy_Funster Posted March 3, 2011 Share Posted March 3, 2011 then just include a less than in the where clause. $qry = "SELECT word, definition FROM dict_list WHERE UCASE(word) LIKE UCASE('$q%') AND CHAR_LENGTH(word) <= 11 LIMIT $limit"; Quote Link to comment https://forums.phpfreaks.com/topic/229476-php-and-mysql-regexp-curly-braces-and-max-characters/#findComment-1182310 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.