Page 1 of 1

Filter a Query

Posted: Fri Apr 15, 2005 9:21 am
by ra
I have a meta search engine that displays recent search queries:

http://www.sodora.com/queries.html

As you can tell, there are a lot of 'naughty' searches being submitted.

I fear that Google will assume my site to be adult-related and cut off my adwords stream.

I'm trying to remove words from a predefined list, but I can't seem to be able to get it done. Any thoughts?

Code: Select all

<?
function format_term($term) {
$term = str_replace('-','~~',$term);
return str_replace(' ','-',$term);
}
$res = mysql_query("SELECT * FROM $querytable ORDER BY q_id DESC") or die(mysql_error());
$bad_words = array('pthc rompl ranchi pthc pthc ranchi board', 'melissa midwest', 'melissa midwest movies');

$pattern = '%'.preg_quote(implode('|', $bad_words)).'%i';

if (preg_match($pattern, $searchstring)) {
do_insert();
}
while (($line = mysql_fetch_assoc($res)) && strlen(ob_get_contents()) < 95*1024) {
echo "<a href='".constant('dir')."search/".format_term($line['q_query'])."'>".$line['q_query']."</a><br>";
}
?>
</div>

Posted: Fri Apr 15, 2005 9:40 am
by feyd
you may want to look at the pattern you are generating.. hint: you have the order of operations mixed up.

Posted: Sat Apr 16, 2005 10:41 pm
by ra
so how should i restructure?

Posted: Sun Apr 17, 2005 6:46 am
by feyd
look at what $pattern turns out to be.

Posted: Sun Apr 17, 2005 10:54 am
by ra
I'm afraid I don't follow... could you expand on that?

Posted: Sun Apr 17, 2005 12:53 pm
by feyd
  1. echo $pattern.
  2. look at the regex logic you request in it
  3. fix your order of operations.

Posted: Sun Apr 17, 2005 1:46 pm
by ra
1. echo $pattern.
Are you saying there is something wrong with my echo pattern?

2. look at the regex logic you request in it
What is regex logic?

3. fix your order of operations.
How?

Posted: Sun Apr 17, 2005 2:23 pm
by John Cartwright

Code: Select all

while (($line = mysql_fetch_assoc($res)) && strlen(ob_get_contents()) < 95*1024) {
Look carefully at your brackets.

Posted: Mon Apr 18, 2005 1:25 pm
by ra
WHat about the brackets? What would it take to get a straight answer?

Posted: Mon Apr 18, 2005 7:39 pm
by feyd
you've gotten several straight answers to errors in your code. Count the open and closing parens in Jcart's post.

Next, if you understand some of the fundamentals of regex, you'll see that you have errors in your pattern for what you intended it to be.

And I'm done trying to help on that.

Posted: Tue Apr 19, 2005 9:26 am
by ra
wow, that's really helpful.

Posted: Tue Apr 19, 2005 11:25 am
by vigge89
ra wrote:wow, that's really helpful.
Wait, are you joking or are you serious?

Posted: Sun Apr 24, 2005 12:23 am
by ra
joking

Posted: Sun Apr 24, 2005 12:34 am
by ra
feyd wrote:you've gotten several straight answers to errors in your code. Count the open and closing parens in Jcart's post.

Next, if you understand some of the fundamentals of regex, you'll see that you have errors in your pattern for what you intended it to be.

And I'm done trying to help on that.
WHen i 'fix' the code according to jcart's suggestion, the script does not work. This is a functional script that i am trying add a word filter to...

And i don't understand any of the fundamentals of regex. that was one of my questions.

Posted: Sun Apr 24, 2005 12:36 am
by ra
The added script is:

Code: Select all

$bad_words = array('pthc rompl ranchi pthc pthc ranchi board', 'melissa midwest', 'melissa midwest movies');
 
$pattern = '%'.preg_quote(implode('|', $bad_words)).'%i';
everyhting else is pre-existing and fully functional.