hadeosdin Posted October 21, 2009 Share Posted October 21, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/178528-word-filter-problem/ Share on other sites More sharing options...
salathe Posted October 22, 2009 Share Posted October 22, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/178528-word-filter-problem/#findComment-941846 Share on other sites More sharing options...
hadeosdin Posted October 23, 2009 Author Share Posted October 23, 2009 im trying to get 2 values from a db and then filter submissions from words useing the db tried to get the words to filter and the filter from the db. and i have already tried what you sugested but it didnt work unless i did it wrong. thanks for the post! Quote Link to comment https://forums.phpfreaks.com/topic/178528-word-filter-problem/#findComment-943184 Share on other sites More sharing options...
.josh Posted October 23, 2009 Share Posted October 23, 2009 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' Quote Link to comment https://forums.phpfreaks.com/topic/178528-word-filter-problem/#findComment-943190 Share on other sites More sharing options...
hadeosdin Posted October 23, 2009 Author Share Posted October 23, 2009 ok then as im newbie to all this can i ask if anyone knows how i would get 2 values from a db and then use value 1 as a word to search for and value 2 to change the word that is found Quote Link to comment https://forums.phpfreaks.com/topic/178528-word-filter-problem/#findComment-943191 Share on other sites More sharing options...
DavidAM Posted October 23, 2009 Share Posted October 23, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/178528-word-filter-problem/#findComment-943199 Share on other sites More sharing options...
hadeosdin Posted October 23, 2009 Author Share Posted October 23, 2009 hey thanks alot for your reply but still i get nothing changed on any off the words its becoming quite the pain in the bum Quote Link to comment https://forums.phpfreaks.com/topic/178528-word-filter-problem/#findComment-943205 Share on other sites More sharing options...
DavidAM Posted October 24, 2009 Share Posted October 24, 2009 can you post your code as it is not so we can have a look? Quote Link to comment https://forums.phpfreaks.com/topic/178528-word-filter-problem/#findComment-943271 Share on other sites More sharing options...
Dorky Posted October 24, 2009 Share Posted October 24, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/178528-word-filter-problem/#findComment-943282 Share on other sites More sharing options...
hadeosdin Posted October 27, 2009 Author Share Posted October 27, 2009 ok now i have got it working but if there is a space in the word like s wear w o rd it dose not pick it up how would i go about fixing this? Quote Link to comment https://forums.phpfreaks.com/topic/178528-word-filter-problem/#findComment-945542 Share on other sites More sharing options...
hadeosdin Posted October 30, 2009 Author Share Posted October 30, 2009 any one got any idears? Quote Link to comment https://forums.phpfreaks.com/topic/178528-word-filter-problem/#findComment-947781 Share on other sites More sharing options...
hadeosdin Posted November 2, 2009 Author Share Posted November 2, 2009 sorry to be a pain but some one has sureley got to know Quote Link to comment https://forums.phpfreaks.com/topic/178528-word-filter-problem/#findComment-949328 Share on other sites More sharing options...
cags Posted November 2, 2009 Share Posted November 2, 2009 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). Quote Link to comment https://forums.phpfreaks.com/topic/178528-word-filter-problem/#findComment-949338 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.