Jump to content

Recommended Posts

There probably are plenty of scripts that have it, but there's no built in function as such...you'd be best to create an array of all the words you wanted to filter out, then loop through that array, checking the content of the string you wish to censor for a match each time.

I have been working on just the same thing for the last day with some success until I stopped handling words as absolute array values since people could change capitalization within words and the like. So here is what I made, it is pretty simple but it catches all variants. I assume you have mySql and know how to connect.

 

<?php
//Set up string to have all your variables to be parsed seperated only by a regular space no commas
$p_string = "$pa $pb $pc $pd";

//Split all posted words into single words to compare against bannned words		
$a11 = preg_split("/[\s,]+/", $p_string, -1, PREG_SPLIT_NO_EMPTY);
	foreach($a11 as $z21);

$con = mysql_connect($HOST, $USER, $PASS); 
if (!$con) { 

  die("Error connecting to MySQL database!"); 
} 
mysql_select_db($NAME, $con);

//Here I query the 'table' for the 'column'
$query = mysql_query("SELECT * FROM badwords WHERE cussword LIKE '%$z21%'"); 
$num_rows = mysql_num_rows($query);

if ($num_rows >= "1")
{

	$message = "<h3 align='center'>This is a place for communicating, please rethink what you are submitting and try again</h3>";

//Here I unset all variables to fail the next validation and make them fill the form out again		
	unset($pa, $pb, $pc, $pd);	
}
?>

 

I know that the a11 and z21 looks weird but I was having intermittent problems with using such sort variable names so I just added 1. The real functionality of this comes from the sql query 'LIKE' wich will match words without being case sinsitive.

I have a text file that I want to filter out all the bad

language. I could use a database but in this case I am

using a text file. Every line of code below works, but I

have no way to filter it.

 

Expamle of this code


<form method="POST">
<table border="1" align="center" width="300">
  <tr>
    <td colspan="2" align="center">How to Change the World?</td>
  </tr>
  <tr>
    <td align="center"><img src="../images/atlas.jpg" width="100" height="165" /></td>
    <td><textarea name = "content" cols="50" rows="10"></textarea></td>
  </tr>
  <tr>
    <td colspan="2">
<?
if(isset($_POST['content'])) { //Page was submitted
    if (!$_POST['content']) { //value of content is empty
        $output = "There was no data entered!";
	echo $output;

    } else { //Content has value
        $content = $_POST['content'];
        $info = $_POST['info'];

        @$fp = fopen("project4.txt", "a");
  
        fwrite($fp, $content."\r\n");
        
        $info = file('project4.txt');
	//Array 
	$info = array("curse","word"," foul ","language"); 
        for ($i = 0; $i < count($info); $i++) {
           echo '<br><hr>';
           echo nl2br(htmlentities(stripslashes(trim($info[$i])),ENT_QUOTES));
        }
        fclose($fp);
    }
}
?>
</td>
  </tr>
  <tr>
    <td colspan="2" align="center"><input type = submit name="submit"></td>
  </tr>
</table>
</form>

 

This is the code that I got from phpfreaks tutorial but I

have no idea on how to insert this into my code where

it will work.

function language_filter($string) { 
    $obscenities = @file("path/to/your/file/foul_language.txt"); 
    foreach ($obscenities as $curse_word) { 
        if (stristr(trim($string),$curse_word)) { 
            $length = strlen($curse_word); 
            for ($i = 1; $i <= $length; $i++) { 
                $stars .= "*"; 
            } 
            $string = eregi_replace($curse_word,$stars,trim($string)); 
            $stars = ""; 
        } 
    } 
    return $string; 
} 

$txt = "I swear that this swear filter is swearing me dead!";

$txt = swear_remover($txt);

function swear_remover($text) {

$text = eregi_replace('swear', '****', $text);
$text = eregi_replace('swear', '****', $text);
$text = eregi_replace('swear', '****', $text);

return $text;
}

<form method="POST">
<table border="1" align="center" width="300">
  <tr>
    <td colspan="2" align="center">How to Change the World?</td>
  </tr>
  <tr>
    <td align="center"><img src="../images/atlas.jpg" width="100" height="165" /></td>
    <td><textarea name = "content" cols="50" rows="10"></textarea></td>
  </tr>
  <tr>
    <td colspan="2">
<?
function language_filter($string) { 
    $obscenities = @file("path/to/your/file/foul_language.txt"); 
    foreach ($obscenities as $curse_word) { 
        if (stristr(trim($string),$curse_word)) { 
            $length = strlen($curse_word); 
            for ($i = 1; $i <= $length; $i++) { 
                $stars .= "*"; 
            } 
            $string = eregi_replace($curse_word,$stars,trim($string)); 
            $stars = ""; 
        } 
    } 
    return $string; 
} 
if(isset($_POST['content'])) { //Page was submitted
    if (!$_POST['content']) { //value of content is empty
        $output = "There was no data entered!";





echo $output;

    } else { //Content has value
        $content = language_filter($_POST['content']);
        $info = $_POST['info'];

        @$fp = fopen("project4.txt", "a");
  
        fwrite($fp, $content."\r\n");
        
        $info = file('project4.txt');





//Array 





$info = array("curse","word"," foul ","language"); 
        for ($i = 0; $i < count($info); $i++) {
           echo '<br><hr>';
           echo nl2br(htmlentities(stripslashes(trim($info[$i])),ENT_QUOTES));
        }
        fclose($fp);
    }
}
?>



</td>
  </tr>
  <tr>
    <td colspan="2" align="center"><input type = submit name="submit"></td>
  </tr>
</table>
</form>

 

There you go.

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.