Jump to content

Recommended Posts

PHPfreaks your script woks fine. However I would like to make a few modifications and apply it inside input fields, how do I do that? Lets say my input fields are as foolows:

 

<input type='text' name='badwords' /> <!-- where do I put php on this input field as a php include file-->

 

<?php

$badwords = array('bad1', 'bad2', 'bad3', 'ass');

$text = 'This is a test. Ass. Grass. bad1.';

function filterBadwords($text, array $badwords, $replaceChar = '*') {
    return preg_replace_callback(
        array_map(function($w) { return '/\b' . preg_quote($w, '/') . '\b/i'; }, $badwords),
        function($match) use ($replaceChar) { return str_repeat($replaceChar, strlen($match[0])); },
        $text
   
);
}

echo filterBadwords($text, $badwords);

?>

Link to comment
https://forums.phpfreaks.com/topic/282159-bad-words-filtering/
Share on other sites

Are you talking about adding bad words to a bad words list?

 

Not tested- May not be error free

 

<?php
if(!empty($_POST['badwords'])) {
 if(strpos(',',$_POST['badwords']) !== false) {
  $arr = explode(',',$_POST['badwords']);
  $str = implode(PHP_EOL,$arr);
 } else {
  $str = $_POST['badwords'] . PHP_EOL;
 }
file_put_contents('badwords.txt',$str,FILE_APPEND);
}
 
if(file_exists('badwords.txt')) {
 $badwords = file('badwords.txt');
} else {
 $badwords = array();
}
 
$text = 'This is a test. Ass. Grass. bad1.';

function filterBadwords($text, array $badwords, $replaceChar = '*') {
    return preg_replace_callback(
        array_map(function($w) { return '/\b' . preg_quote($w, '/') . '\b/i'; }, $badwords),
        function($match) use ($replaceChar) { return str_repeat($replaceChar, strlen($match[0])); },
        $text
    );
}

echo filterBadwords($text, $badwords);
 
?>
<form action="" method="post">
<label for="badwords">Add a Word to the Filter</label><br />
<input type="text" name="badwords" />
<input type="submit" name="submit" value=" ADD " />
</form>
Link to comment
https://forums.phpfreaks.com/topic/282159-bad-words-filtering/#findComment-1449543
Share on other sites

Hi guru

I meant how go I get users input to replace any badwords with symbols? My biggest problem is using input field with PHP, as a newbie I am struggling a bit so any help would be welcomed and appreciated greatly. I mean the above example for instance the badwords.text am i correct to add bad words in the file as follows: "bad1","bad2","bad3"

or do I have to enter badwords this way: "bad1, bad2, bad3"

 

I want to make sure that when user type in a bad word in the field it alerts them that thier badword will be replaced with ***

Thank you once again

Link to comment
https://forums.phpfreaks.com/topic/282159-bad-words-filtering/#findComment-1449713
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.