Filter a Query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ra
Forum Commoner
Posts: 58
Joined: Fri Mar 25, 2005 4:25 pm

Filter a Query

Post 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>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you may want to look at the pattern you are generating.. hint: you have the order of operations mixed up.
ra
Forum Commoner
Posts: 58
Joined: Fri Mar 25, 2005 4:25 pm

Post by ra »

so how should i restructure?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

look at what $pattern turns out to be.
ra
Forum Commoner
Posts: 58
Joined: Fri Mar 25, 2005 4:25 pm

Post by ra »

I'm afraid I don't follow... could you expand on that?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

  1. echo $pattern.
  2. look at the regex logic you request in it
  3. fix your order of operations.
ra
Forum Commoner
Posts: 58
Joined: Fri Mar 25, 2005 4:25 pm

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

while (($line = mysql_fetch_assoc($res)) && strlen(ob_get_contents()) < 95*1024) {
Look carefully at your brackets.
ra
Forum Commoner
Posts: 58
Joined: Fri Mar 25, 2005 4:25 pm

Post by ra »

WHat about the brackets? What would it take to get a straight answer?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
ra
Forum Commoner
Posts: 58
Joined: Fri Mar 25, 2005 4:25 pm

Post by ra »

wow, that's really helpful.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

ra wrote:wow, that's really helpful.
Wait, are you joking or are you serious?
ra
Forum Commoner
Posts: 58
Joined: Fri Mar 25, 2005 4:25 pm

Post by ra »

joking
ra
Forum Commoner
Posts: 58
Joined: Fri Mar 25, 2005 4:25 pm

Post 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.
ra
Forum Commoner
Posts: 58
Joined: Fri Mar 25, 2005 4:25 pm

Post 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.
Post Reply