Jump to content

Recommended Posts

Hello,

 

im trying to make a filter for words inputted in to my website but i want to store the swear word and the replacement for that word in a mysql db i have developed the code below;

 

<?php

 

include("config.php");

 

 

$str = 'how are you have you got any money';

 

 

 

 

$result = mysql_query("SELECT * FROM table")

or die(mysql_error()); 

 

while($row = mysql_fetch_array( $result )) {

$words = array($row['word'],);

$filter = array($row['translated_word'],);

 

 

}

echo str_replace($words, $german, $str);

?>

 

but it just dose not return the filters how i need it to it will only list it as it returns them like

 

wordwordwordwordword

 

but i need it to return them like

 

word,word,word,word,word

 

how would i do this? please help me =] thanks

Link to comment
https://forums.phpfreaks.com/topic/178528-word-filter-problem/
Share on other sites

The question and description are unclear, what exactly is the problem? 

 

However, I would offer one change to how you construct what I assume should really be an array of words and their translations.

 

while($row = mysql_fetch_array( $result )) {
     $words[] = $row['word'];
     $filter[] = $row['translated_word'];
}
echo str_replace($words, $filter, $str);

Link to comment
https://forums.phpfreaks.com/topic/178528-word-filter-problem/#findComment-941846
Share on other sites

I don't see how the code you posted could possibly output what you posted, unless you posted the code wrong (for instance, is that echo really inside that loop?).  But anyways, you are claiming it is outputting

 

wordwordwordwordword

 

but you want

 

word,word,word,word,word

 

so...just concat a comma at the end of...wherever you're outputting 'word'

Link to comment
https://forums.phpfreaks.com/topic/178528-word-filter-problem/#findComment-943190
Share on other sites

If I was doing this, I would create a table with two columns: BadWord and FixWord. Make BadWord a unique index (or the primary key).  Then load the words like this (this code assumes an existing connection to the database):

$filter = array();  // empty filter array to start with
$res = mysql_query('SELECT BadWord, FixWord FROM WordFilter');
while ($row = mysql_fetch_array($res)) {
  $filter[$row[0]] = $row[1];
}
mysql_free_result($res);
// Now you have a filter array with the key being the bad word

// Get the user's input
$inString = $_GET['txtInput']; // or whatever
$words = explode(' ', $inString);
$outString = '';

// walk the array of words looking for bad words
foreach($words as $word) {
  if (array_key_exists($word, $filter)) {
    $outString .= $filter[$word] . ' ';
  else
    $outString .= $word . ' ';
}
// Now $outString contains the censored input
// (with an extra space at the end)
echo $outString . PHP_EOL;

 

This code will do what you asked.  With a couple of caveats.

  • It is not case insensitive (i.e. Damn and damn are two different words)
  • It will not catch words followed by punctuation (i.e. Damn, I left the bitch.)

I leave those exercises to the reader.

 

I'm just wondering if this is the best way to do it.  If your badwords table gets big, you are loading a lot of data into an array that you will not use.  However, I can't think of a way to accomplish this in SQL right now.  I might consider testing something along these lines:

 

SELECT BadWord, FixWord FROM WordFilter 
WHERE '$UserInput' LIKE '%' + BadWord + '%';

If I could get this syntax to work, the select would only return the words that need to be replaced.  That would reduce the amount of data returned to PHP and reduce the overhead of the page.  Of course, I don't have spaces around the BadWord, so 'class' might get censored (easily fixed); but the table can be setup to be insensitive to case.

Link to comment
https://forums.phpfreaks.com/topic/178528-word-filter-problem/#findComment-943199
Share on other sites

i believe you are trying to filter cuss words, correct? a db is way overkill for this.

you can do all of this by creating an array with all of the words to be filtered and using

str_replace(

go here to better understand the depth of the command

http://us2.php.net/str_replace

 

 

Link to comment
https://forums.phpfreaks.com/topic/178528-word-filter-problem/#findComment-943282
Share on other sites

There is no one solution fits all. If people wanna swear there is fuck all you can do to stop them. There is always someway they can modify a word so that it looks like a swear word without matching in a swear filter. Whether that is by adding spaces (shi t etc.), character substitution (a$$, b!tch etc.). str_replace is a poor choice for such a method because of false positives (for example it might turn the ass part of mass to bottom leaving you with mbottom). You can use regular expressions to limit this false positive effects as regular expressions can match things like word boundarys. Using regular expressions you could search for specific charactersequences that could ignore spaces, but you will just be introducing a greater likelyhood of false positives (eg, "To open a door, you push it", would be considered to contain the word shit).

Link to comment
https://forums.phpfreaks.com/topic/178528-word-filter-problem/#findComment-949338
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.