Any questions involving matching text strings to patterns - the pattern is called a "regular expression."
Moderator: General Moderators
rubberjohn
Forum Contributor
Posts: 193 Joined: Fri Feb 25, 2005 4:03 am
Post
by rubberjohn » Wed Mar 22, 2006 5:06 pm
I'm trying to make a simple search function that takes a string of user input and returns matches based on the number of keywords that match.
I am basing this on code that is about 5 years old - is it still valid as I am having trouble getting the REGEX to work? I am using PHP Version 4.4.2.
Code: Select all
function search($query_terms){
$tags = explode(' ', addslashes(strtolower($tags)));
$reg_ex = "[(" . implode(")(", $tags) . ")]";
$sql_get_tags =mysql_query( "SELECT count(f_tags.tag_id) as score,
$tag_link.user_id as f_user_id
FROM f_tags, $tag_link
WHERE ($tag_link.tag_id = f_tags.tag_id)
AND (f_tags.tag REGEXP $reg_ex)") or die (mysql_error());
while($get_tags = mysql_fetch_array($sql_get_tags)) {
print_r($get_tags);
}
}
Thanks in advance
rj
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed Mar 22, 2006 5:17 pm
It's not valid for MySQL's REGEXP, although it appears $tags magically appears.
You may want to test if LIKE or FULL TEXT works better for this.
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Wed Mar 22, 2006 9:09 pm
Maybe it's out of a class. You may need to add $tags to the function parameters if it isn't anymore.
rubberjohn
Forum Contributor
Posts: 193 Joined: Fri Feb 25, 2005 4:03 am
Post
by rubberjohn » Thu Mar 23, 2006 6:56 am
so if a LIKE statement was used instead:
Code: Select all
SELECT "SOMETHING"
FROM "TABLE"
WHERE "SOMETHING" LIKE {PATTERN}
I know PATTERN can be a variable for finding a single item. If you had multiple search items which could vary in number, would it be best to use an array in a for_each?
thanks
rj
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Thu Mar 23, 2006 10:14 am
Code: Select all
SELECT `field`
FROM `table`
WHERE `field` LIKE '%{$term1}%'
OR `field` LIKE '%{$term2}%'
etc etcHowever, it could slow down the query. You should probably do several tests to find out which works best for what you need.
rubberjohn
Forum Contributor
Posts: 193 Joined: Fri Feb 25, 2005 4:03 am
Post
by rubberjohn » Thu Mar 23, 2006 10:28 am
i think it probably would because the number of keywords submitted could be a lot