Jump to content
xisto Community
KansukeKojima

Php Word Filter Have you accidently sworn on your site? Or do you want to keep visitor

Recommended Posts

This is pretty simple but very useful if you don't want people to swear. We will be using str_replace for this.

<?phpstr_replace ("curseword, "replacemet");?>

Thats pretty simple, just fill in the curse word and the replacement, and then repeat... heres what it would look like full size:
<?phpstr_replace("swear", "replacement");str_replace("swear", "replacement");str_replace("swear", "replacement");str_replace("swear", "replacement");?>

Ok, but how do you get it to work on your page now? Well, save that file as wordfilter.php and use
<?php include "wordfilter.php"; ?>
at the top of your page... and now people can't swear...

By the way, I'm pretty sure these are case sensitive, so you'll have to put capitals as well, etc..

Share this post


Link to post
Share on other sites

Although the str_replace function is one way to filter out certain words from strings, the example you provided would not work if actually used on a site for the following reason: subject parameter is not set. I'm quite sure PHP would output an error similar to that if tested. For case insensitivity, you could use str_ireplace.

Share this post


Link to post
Share on other sites

This will not work at all. There is no origin of content to replace from and no variable to actually echo. This would be more correct. This will be a chatbox example.

<?php$shout=strip_tags($_POST['shout']);$shouts=str_replace("curse","replace",$shouts);//Repeat each word//mysql query?>

That would be the correct form. Your example will simply provide an error. If it were to work it would still display the curse word due to no variable being stored.

Share this post


Link to post
Share on other sites

I guess so eh....... to do this really fast, you could do what I said in the "Nice clean php coding" thread, then you could just strip from the $template variable, and when you echo it, the swears, and whatever are gone.....

Share this post


Link to post
Share on other sites

This will not work at all. There is no origin of content to replace from and no variable to actually echo. This would be more correct. This will be a chatbox example.

<?php$shout=strip_tags($_POST['shout']);$shouts=str_replace("curse","replace",$shouts);//Repeat each word//mysql query?>

That would be the correct form. Your example will simply provide an error. If it were to work it would still display the curse word due to no variable being stored.

I think the $shout variable should be $shout and not $shouts as the parameter to the str_replace function.

Share this post


Link to post
Share on other sites

PHP Form Fillter

Php Word Filter

 

Dear Sir,

 

How define fillter on Data base Form Fields (such as Empty Feilds,only enter integer value etc.)

 

Thanks

 

-question by Arshad

Share this post


Link to post
Share on other sites

Well, a good tutorial? Is it possible to modify it or not?If some registered user used bad words on some certain web-site, the script automatically blocks his or her account for 1 day, but if it happens again, it deletes his or her profile from entire system?!

Share this post


Link to post
Share on other sites

Well, a good tutorial? Is it possible to modify it or not?
If some registered user used bad words on some certain web-site, the script automatically blocks his or her account for 1 day, but if it happens again, it deletes his or her profile from entire system?!


It would easily be possible to modify this code to do that, but as we have no information about the system you're integrating it with, we can't provide any real code, just the ideas:

You'd need to use the return value of the str_replace function to work out if any replacements have occurred.
If replacements have occurred, the user swore, so ban their account for 1 day.
Place a permanent mark in your database against that user to say they have used their first chance.
If replacements have occurred, and the user is marked as having used their first chance, ban/delete them permanently.

Share this post


Link to post
Share on other sites

We'll say we are submitting a post to a bb via POST with the name 'message'.Have a file containing a curse and replacement on each line called filter.TxtSo file looks like:Curse!replacementCurse!replacementCurse!replacement...$data = file('filter.Txt'); // pull filters in to arrayForeach($file as $val){ // step through filter array $ding = explode('!', $ding); // explode each line in to curse and replace $_POST['message'] = str_ireplace($ding[0], $ding[1], $_POST['message']); // case insensitive replacement}-reply by Kodosai

Share this post


Link to post
Share on other sites

You could also use the ob functions, with these you could write a function for PHP word filter and then use ob_start to apply it to everything that's written to the users screen. And then at the end use ob_flush to display the page. I'm sure you can find a lot of topics on PHP ob if you look in google.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.