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
https://forums.phpfreaks.com/topic/23710-need-help-with-lang-filter/
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]

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.