Jump to content

[SOLVED] php language filter


Trium918

Recommended Posts

I am trying to create an advance language filter.

Ok, I am using the language filter that I got from

phpfreaks tutorial, but it only check the words that

are in the array $obscenities, curse, word etc..

 

I want it where it reads each character

Example: c*u*r*s*e

 

<?
session_start();
function language_filter($string) { 
   $obscenities = array("curse","word"," foul ","language"); 
    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
//header("Location: http://localhost/project4.php");
} else { //Content has value
        $info = $_POST['info'];    
        $content = language_filter($_POST['content']); 
        $fp = fopen("project4.txt", "a");
        fwrite($fp, $content."\r\n");
	fclose($fp);
	$_SESSION = 'isset';
	header( "Location: " .$_SERVER['PHP_SELF'] ); //Have to go before any HTML
    }
} 

?>

<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($_SESSION)) {
	//echo 'Form was posted';
        $fp = fopen("project4.txt", "a");
        $info = file('project4.txt');
        for ($i = 0; $i < count($info); $i++) {
           echo '<br><hr>';
           echo nl2br(htmlentities(stripslashes(trim($info[$i])),ENT_QUOTES));
        }
        fclose($fp);
	unset($_SESSION);
	}
?>

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

Link to comment
https://forums.phpfreaks.com/topic/44564-solved-php-language-filter/
Share on other sites

what's a banana????

 

This is a regex issue and one that you can simply keep adding to.

 

You could do something like creating an arry of words to filter and then using those in a reg ex to do the donkey work.

<?php
$filter = array('curse','swear','execrate');

$regex ='/';
foreach($filter as $key => $val)
{
$regex .='(';
while($i < strlen($val))
{
  $regex .= '[^a-zA-Z0-9]*?'.$val{$i++};
}
$regex .= '[^a-zA-Z0-9]?|';
}

$regex .= substr($regex, 0, -1) . '/i';
$string = preg_replace($regex,'***',$string);

 

Now that regex (if it even works) will do all kinds of wonderful and unexpected things probably. Have a play see how you get on.

I donnot know where to start. thrope, where would you start just give

me a starting point. Maybe I can build from that.

 

My guess would be here:

if (stristr(trim($string),$curse_word))

 

I understand that I need a function that would and a loop

that reads each character even, and pull out the letters

c u r s e. Notice that there are whitespace characters in

this string.

a machine needs a pattern to spit things out, a method persay

 

either you enlighten yourself on a similar pattern that people always use when they try to sneak in curse words

 

OR

you think up of all the different types of "sneaking in curse words" people will try, and incorporate that into your regex,

 

for example you could think up that people might try to swear like these couple of ways:

1. fuck

2. f.u.c.k

3. f*u*c*k

 

etc...., you can't just match the letters f, u, c, and k with regex because then it will find words that are in fact NOT curse words, and you can't simply filter out "fuck" because people might try to sneak it in

 

so you are left with creating regexes for the most popular ways people might try to sneak those curse words in, this is not a simple task

Ok, have anyone played Madden or any other online game?

They have a great language filter system. I am not able to enter W*O*R*D

if that word is being filter. The output would be like @%*$#!&. Every character

is replace with another character. Is there a software that I can use.

Regular Expressions are your best method of implementing a language filter.

 

I recommend taking the time to read up on it, because you'll find many uses for it once you understand how powerful it is.

 

There are hundreds of tutorials out there that will help you get a hang of it, including video tutorials:

 

http://www.phpvideotutorials.com/regex/

 

Good luck!

 

:)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.