douxiangbiao1899 2017-06-23 05:28
浏览 48

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

How can i execute my Function into text area to change the swearwords into asterisk in database.

this is the Filter.

<?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);
}
?>

and this is the one that will insert the reply into the database. `

<?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';
?>

and this is the function to reply.

<?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';
?>

Summary : How can I turn the reply swear words into asterisk in the database. any approach is okay, and every help is much appreciated thank you and Godbless. :)

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 WPF 大屏看板表格背景图片设置
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂
    • ¥15 wordpress 产品图片 GIF 没法显示