Jump to content

Archived

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

oracle259

need help with lang filter

Recommended Posts

Hi i got this code from the freaks tutorial section. How can i modify it so that it gets the obsenities from a mysql database without losing the ability to filter ie *** multiple curse words in a single input eg buttsagass should give ****sag***

[code]
<?php
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;
}
?>
[/code]

Share this post


Link to post
Share on other sites
You can try this.  Need to set the database info.
[code]
<?php
function language_filter($string)
{
// Connect to the database
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link)
die("Could not connect to the database\n" . mysql_error());

// Select the database
$db_selected = mysql_select_db('database_name', $link);
if (!$db_selected)
die("Can't select database\n" mysql_error());

// Select the words to filter
$request = mysql_query("SELECT colName FROM tblName");

// Define $obscenities and loop through the results and assign it to $obscenities
$obscenities = array();
while ($row = mysql_fetch_assoc($request))
$obscenities[] = $row['colName'];

// Free result abd close the connection
mysql_free_result($request);
mysql_close($link);

foreach ($obscenities as $curse_word)
{
if (stristr(trim($string),$curse_word))
{
$length = strlen($curse_word);
$stars = '';
for ($i = 1; $i <= $length; $i++)
$stars .= '*';

$string = eregi_replace($curse_word, $stars, trim($string));
$stars = '';
}
}

return $string;
}
?>
[/code]

Share this post


Link to post
Share on other sites
Something went wrong it does retrieve the info from the mysql database but it only replaces one curse word eg. assmanbutt should return ***man**** but it only returns assman**** do u have any ideas on how to fix this problem

Share this post


Link to post
Share on other sites
I just tested it locally and it worked.  Check to see that you actually have that word in the database.

Share this post


Link to post
Share on other sites
thanks i finally got it to work the way i want it to but i still have one more problem. How can i save the name of each curse_word replaced with *** in an array.

Share this post


Link to post
Share on other sites

×

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.