我怎样才能将进入Mysql的咒骂词过滤成星号

时间:2017-06-23 05:28:51

标签: php mysql

如何将我的功能执行到文本区域以将脏话改为数据库中的星号。

这是过滤器。

<?php

function SmartCensor($string)
{
    /**
     * Config Arrays
     **/

    //Words used to split swears, i.e. a.s.s
    $illegal = array("!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "_", "+",
        "=", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", ",", ".", "/", "\\", "{",
        "}", "[", "]", "~", "`", ">", "<", ";", ":", "'", "\"", "?", "|");

    //Swears and their replacements
    $BadWords = array
    ("a.ss",
    "s.hit",
    "f.uck",
    "bitc.h",
    "as.shole",
    "fu.cker",
    "sht",
    "btch",
    "f.uk",
    "c.unt");

   $RePlace = array
   ("***",
    "*****", 
    "****",
    "*****",
    "*******",
    "******", 
    "****",
    "*****",
    "****"
    );

    //RegEx to find spaced out swears
    $RegEx = array(
    "(\s?+(a|A)\s?\s?+(s|S)\s?\s?+(s|S)\s+)" => " *** ", //3 letter word
    "(\s?+(s|S)\s?\s?+(h|H)\s?\s?+(i|I)\s?\s?+(t|T)\s?+)" => " **** ", // 4 letter word
    "(\s?+(f|F)\s?\s?+(u|U)\s?\s?+(c|C)\s?\s?+(k|K)\s?+)" => " **** ",
    "(\s?+(b|B)\s?\s?+(i|I)\s?\s?+(t|T)\s?\s?+(c|C)\s?\s?+(h|H)\s?+)" => " ***** ", // 5 letter word
    "(\s?+(a|A)\s?\s?+(s|S)\s?\s?+(s|S)\s?\s?+(h|H)\s?\s?+(o|O)\s?\s?+(l|L)\s?\s?+(e|E)\s?+)" => " ******* ",
    "(\s?+(f|F)\s?\s?+(u|U)\s?\s?+(c|C)\s?\s?+(k|K)\s?\s?+(e|E)\s?\s?+(r|R)\s?+)" => " ****** ",
    "(\s?+(s|S)\s?\s?+(h|H)\s?\s?+(t|T)\s?+)" => " **** ",
    "(\s?+(b|B)\s?\s?+(t|T)\s?\s?+(c|C)\s?\s?+(h|H)\s?+)" => " ***** ",
    "(\s?+(f|F)\s?\s?+(u|U)\s?\s?+(k|K)\s?+)" => " **** "
   );

    /**
     *Start Process 
     **/

     //(1) Take care of spaced out swears via regex
      $string = preg_replace(array_keys($RegEx), array_values($RegEx), $string);

     //EXPODE: Seperate the string word by word
     $ex = explode(" ", $string);

    //Alternate letters, e.g. @ for A, $ for S
    $alt = array("@", "!", "$", "|", "0");
    $real = array("a", "i", "s", "i", "o");

    //(2) Get rid of string seperators, check for swears
    for ($i = 0; $i < sizeof($ex); $i++) {
        $x = str_ireplace($illegal, "", $ex[$i]);
        if (in_array(strtolower($x), $BadWords)) {
            $ex[$i] = str_ireplace($BadWords, $RePlace, $x);
        }
    }

    //(3) Check for alternate spelling with special chars
    for ($i = 0; $i < sizeof($ex); $i++) {
        $y = str_ireplace($alt, $real, $ex[$i]);
        if (in_array(strtolower($y), $BadWords)) {
            $ex[$i] = str_ireplace($BadWords, $RePlace, $y);
        }
    }


    return implode(" ", $ex);
}
?>

这是将回复插入数据库的那个。 `

<?php
//create_cat.php
session_start();
error_reporting (0);
include 'connect_to_mysql.php';
include 'header.php';
include 'filter.php';




$sql = "SELECT
            topic_id,
            topic_subject
        FROM
            topics
        WHERE
            topics.topic_id = " . mysql_real_escape_string($_GET['id']);

$result = mysql_query($sql);

if(!$result)
{
    echo 'The topic could not be displayed, please try again later.';
}
else
{
    if(mysql_num_rows($result) == 0)
    {
        echo 'This topic doesn&prime;t exist.';
    }
    else
    {
        while($row = mysql_fetch_assoc($result))
        {
            //display post data
            echo '<table class="topic" border="1">
                    <tr>
                        <th colspan="2">' . $row['topic_subject'] . '</th>
                    </tr>';

            //fetch the posts from the database
            $posts_sql = "SELECT
                        posts.post_topic,
                        posts.post_content,
                        posts.post_date,
                        posts.post_by,
                        users.user_id,
                        users.user_name
                    FROM
                        posts
                    LEFT JOIN
                        users
                    ON
                        posts.post_by = users.user_id
                    WHERE
                        posts.post_topic = " . mysql_real_escape_string($_GET['id']);

            $posts_result = mysql_query($posts_sql);

            if(!$posts_result)
            {
                echo '<tr><td>The posts could not be displayed, please try again later.</tr></td></table>';
            }
            else
            {

                while($posts_row = mysql_fetch_assoc($posts_result))
                {
                    echo '<tr class="topic-post">
                            <td class="user-post">' . $posts_row['user_name'] . '<br/>' . date('d-m-Y H:i', strtotime($posts_row['post_date'])) . '</td>
                            <td class="post-content">' . htmlentities(stripslashes($posts_row['post_content'])) . '</td>
                          </tr>';
                }
            }

            if(!$_SESSION['signed_in'])
            {
                echo '<tr><td colspan=2>You must be <a href="signin.php">signed in</a> to reply. You can also <a href="signup.php">sign up</a> for an account.';
            }
            else
            {
                //show reply box
                echo '<tr><td colspan="2"><h2>Reply:</h2><br />
                    <form method="post" action="reply.php?id=' . $row['topic_id'] . '">
                        <textarea name="reply-content"></textarea><br /><br />
                        <input type="submit" value="Submit reply" />
                    </form></td></tr>';
            }

            //finish the table
            echo '</table>';
        }
    }

}

include 'footer.php';
?>

这是回复的功能。

<?php
//create_cat.php
session_start();
error_reporting (0);
include 'connect_to_mysql.php';
include 'header.php';
include 'filter.php';
$_SESSION $SmartCensor = ['reply-content']; 
if($_SERVER['REQUEST_METHOD'] != 'POST')
{
    //someone is calling the file directly, which we don't want
    echo 'This file cannot be called directly.';
}
else
{
    //check for sign in status
    if(!$_SESSION['signed_in'])
    {
        echo 'You must be signed in to post a reply.';
    }
    else
    {

        //a real user posted a real reply
        $sql = "INSERT INTO 
                    posts(post_content,
                          post_date,
                          post_topic,
                          post_by) 
                VALUES ('" . $_POST['reply-content'] . "',
                        NOW(),
                        " . mysql_real_escape_string($_GET['id']) . ",
                        " . $_SESSION['user_id'] . ")";

        $result = mysql_query($sql);

        if(!$result)
        {
            echo 'Your reply has not been saved, please try again later.';
        }
        else
        {
            echo 'Your reply has been saved, check out <a href="topic.php?id=' . htmlentities($_GET['id']) . '">the topic</a>.';
        }
    }

}

include 'footer.php';
?>

摘要:如何将回复发誓单词转换为数据库中的星号。任何方法都没关系,每一个帮助都非常感谢谢谢你和Godbless。 :)

0 个答案:

没有答案