473,320 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Checking for bad words in a posted comment

Ajm113
161 100+
-Oops, sorry!- I posted in the wrong area! Can someone move this thread please?

Ok, I got a array going with the words I want. So if someone posted any of those words I want the script to show a message. So this is what I have so far:

[PHP]$message = $_POST['message'];

$filter = array("word", "word2", "word3");

if($message == badword)
{

//Show error message

}else{
//post message

}[/PHP]

I know the if line wont work, but I just wanted to throw it out their as a example of what I want to do.

Any suggestions?
Jun 28 '07 #1
7 2430
Atli
5,058 Expert 4TB
I'm moving this thread to the PHP Forums.
Please refrain from posting in the Articles section!

Moderator
Jun 28 '07 #2
Atli
5,058 Expert 4TB
You could do something like this:
Expand|Select|Wrap|Line Numbers
  1. $message = $_POST['message'];
  2. $searchWord = array("word1", "word2", "...");
  3.  
  4. foreach($searchWord as $word)
  5. {
  6.   if(strpos($message, $word) != false) {
  7.     echo "Invalid word detected!";
  8.     exit;
  9.   }
  10. }
  11. echo "Its all good!";
  12.  
Jun 28 '07 #3
pbmods
5,821 Expert 4TB
Heya, Ajm.

Expand|Select|Wrap|Line Numbers
  1. $badwords = array( ... );
  2. $post = implode(' ', array_diff(explode(' ', $post), $badwords));
  3.  
Jun 28 '07 #4
Jankie
58
Heya Pbmods

Would you please mind of you can afford to elaborate a bit on this?

Code:( php )

1. $badwords = array( ... );
2. $post = implode(' ', array_diff(explode(' ', $post), $badwords));

Does this just filter bad words silently or how this would fit in the code above?

Thanks a lot
Jul 2 '07 #5
This is the answer i think...
[PHP]
$string=$_POST['string'];
$bad=array();
foreach($bad as $i){
$string=str_replace($i," CENSORED ",$string);
}
[/PHP]
Have a nice day,
Luke14free
Jul 2 '07 #6
pbmods
5,821 Expert 4TB
Heya, Jankie.
Would you please mind of you can afford to elaborate a bit on this?

Expand|Select|Wrap|Line Numbers
  1. $badwords = array( ... );
  2. $post = implode(' ', array_diff(explode(' ', $post), $badwords));
Does this just filter bad words silently or how this would fit in the code above
This code does filter out the bad words silently. It explode()s the post, using space characters (i.e., creates an array containing every word in the post). Then it removes all words that match any of the *keys* in $badwords (you'll need to make the bad words keys), and then implode()s the array back into a string, reinserting the spaces.

The one problem with this algorithm is that it doesn't take punctuation into account.
Jul 2 '07 #7
kovik
1,044 Expert 1GB
The foreach method would be your best bet, but don't use strpos(). strpos() can return 0, which is equivalent to false. Use stristr(), which will either return the string (if you which to str_replace it) or FALSE, and is case insensitive as well.

pbmods method could be done much faster by using only str_replace() instead of implode and explode.
Jul 2 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Leo P. | last post by:
I am trying to write a spelling checker. Specifically the part that suggests words. From what I've read online, it seems like the preferred way to do this, is to find the double metaphone...
14
by: Frances Del Rio | last post by:
if (parent.frames.main.location == 'mediaselect.html') { I have a very simple frameset, name of frame where I'm checking is 'main'... why is this not working? I mean this is correct syntax,...
1
by: Deb via AccessMonster.com | last post by:
I have a comment field on my form and want to be able to find similar themes within comments that occur the most within a specific complaint code, i.e.: complaint code = 1 and the common theme from...
2
by: Clint Olsen | last post by:
Hello: I posted a thread on comp.programming awhile back asking about an algorithm I implemented on square root. The idea was to use the square root of a prime number as a convenient way to get...
8
by: Luke Matuszewski | last post by:
Hi ! I have faced the problem of checking that the user has entered the unicode letter (not only ASCII set of letters...). It seems that ECMAScript 3rd regular expressions do not include posix...
12
by: Simon | last post by:
Well, the title's pretty descriptive; how would I be able to take a line of input like this: getline(cin,mostrecentline); And split into an (flexible) array of strings. For example: "do this...
16
by: lawrence k | last post by:
I've made it habit to check all returns in my code, and usually, on most projects, I'll have an error function that reports error messages to some central location. I recently worked on a project...
4
by: bigbagy | last post by:
Notes The programs will be compiled and tested on the machine which runs the Linux operating system. V3.4 of the GNU C/C++ compiler (gcc ,g++) must be used. A significant amount coding is...
10
by: Brent | last post by:
I have a list of company names (say, IBM, Corning, General Motors, and another 5,000 of them). If I take a body of text, a news article, for instance, and I want to see which company names...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.