Jump to content

need help with lang filter


oracle259

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]
Link to comment
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]
Link to comment
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.