Current location: Hot Scripts Forums » Programming Languages » PHP » Search Keyword in "" Phrases, i know it's possible but how


Search Keyword in "" Phrases, i know it's possible but how

Reply
  #1 (permalink)  
Old 08-08-05, 11:41 PM
SummerL SummerL is offline
Newbie Coder
 
Join Date: Jul 2005
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Question Search Keyword in "" Phrases, i know it's possible but how

My search should accept "" to create phrases? It allows matching of keywords in " " phrases that is..

It should allow me to search in exact phrases

Records with "crack situated" would be returned
This is a 2mm Crack situated at the DB Engine Latch Cowling

and not
This is a 2mm Crack and it is situated at the DB Engine Latch Cowling.

Now the thing is whenever i do a search, there is this extra \ between them.. The textfield which i key my value in .. "Car situated" was replaced with a \
Advanced Search Keyword: \"Crack situated\"


Code:
 

$Keyword = (isset($_GET['Keyword'])) ? $_GET['Keyword'] : ''; 
$trimmedKeyword = trim($Keyword); 
$trimmedKeyword = stripslashes($trimmedKeyword); 
$trimmedKeyword = str_replace('"', '', $trimmedKeyword); 
//trim whitespace from the stored variable 
$trimmedKeyword = preg_replace('#\s+#',' ',$trimmedKeyword); 
$trimmed_arrayKeyword = explode(" ",$trimmedKeyword); 

// Build SQL Query for each keyword entered 
foreach ($trimmed_arrayKeyword as $trimmKeyword => $word){ 
if(strtolower($word )){ 
$trimmed_arrayKeyword[$trimmKeyword] = "/\b(?<!<)($word)(?!>)\b/i"; 
}//end if 
else $trimmed_arrayKeyword[$trimmKeyword] = '/\b('.preg_quote($word,'/').')\b/i'; 

$queryKeyword = "SELECT * FROM report WHERE AircraftTailNo REGEXP '[[:<:]]{$word}[[:>:]]' OR DefectInfo REGEXP '[[:<:]]{$word}[[:>:]]' OR Rectifications REGEXP '[[:<:]]{$word}[[:>:]]' OR Comments REGEXP '[[:<:]]{$word}[[:>:]]' OR RelatedDoc REGEXP '[[:<:]]{$word}[[:>:]]'";
Reply With Quote
  #2 (permalink)  
Old 08-09-05, 01:16 AM
darkfreak's Avatar
darkfreak darkfreak is offline
Newbie Coder
 
Join Date: Jun 2004
Location: Kuopio, Finland, Europe
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
[edit: Sorry, didn't notice you already had stripslashes... ]

You probably have magic_quotes_gpc setting on in your php.ini, which automatically adds \'s in front of quote marks in GET/POST data. See manual for that.

For quick fix, instead of your

Code:
 
$Keyword = (isset($_GET['Keyword'])) ? $_GET['Keyword'] : '';
You can try

Code:
 
$Keyword = (isset($_GET['Keyword'])) ? stripslashes($_GET['Keyword']) : '';
Reply With Quote
  #3 (permalink)  
Old 08-09-05, 10:41 AM
SummerL SummerL is offline
Newbie Coder
 
Join Date: Jul 2005
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks i actually got that \ thing solved

But i just can't get my codes to do a exact keywords search with the " "

that is to say whenever i do a search "crack situated"(with the " " on) it should only match records that are like this

This is a 2mm Crack situated at the DB Engine Latch Cowling
Reply With Quote
  #4 (permalink)  
Old 08-11-05, 01:30 AM
darkfreak's Avatar
darkfreak darkfreak is offline
Newbie Coder
 
Join Date: Jun 2004
Location: Kuopio, Finland, Europe
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
Your code looks quite complex. Wouldn't it be enough to first strip off the (possibly automatically added) slashes from the trimmed string, then removing the quotation marks and finally, granted you have the string to search in $text, use a query like:

PHP Code:

$query "SELECT * FROM table WHERE field1 LIKE '%$text%' OR field2 LIKE '%$text%' ... OR fieldN '%$text%' "
Of course, making it case-insensitive would require something like:

PHP Code:

$text strtolower($text);

$query "SELECT * FROM table WHERE LOWER(field1) LIKE '%$text%' OR LOWER(field2) LIKE '%$text%' ... OR LOWER(fieldN) '%$text%' "
I hope it helps!

Last edited by darkfreak; 08-11-05 at 01:32 AM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Search Keyword Textfield SummerL PHP 2 07-28-05 02:48 AM
Optional search problem SummerL PHP 3 07-22-05 07:21 AM
SEO Expert Available nakulgoyal Job Offers & Assistance 2 08-14-04 12:38 PM
Declared Functions skipper23 PHP 4 12-17-03 10:06 AM
index page not showing up skipper23 PHP 3 12-15-03 01:10 PM


All times are GMT -5. The time now is 07:01 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.