字符串非法替换

前端:guestBook.php
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>留言界面</title>
    <style type="text/css">
        body{margin: 0px;padding: 0px;}
        .biaodian{font-family: "宋体";font: 18px;color: green;margin: 0 auto;}
       tr td{border: 1px solid black;width: 300px;height: 40px;}
        #wenti{width: 500px;height: 80px;}
        #tijiao{margin-left:500px ;color: dodgerblue;margin-top: 20px;}
        .CountChar{position: relative;left: 80px;}/*统计字符css*/
    </style>


</head>
<body onload="runtime()">
<form action="showMessage.php" name="form1" method="post">
<table class="biaodian">
    <tr>
        <td >留言标题:<input type="text" name="biaoti" id="liubiao" onblur="checkTitle(this.value)"> </td>
        <td>留言时间:
            <input type="text" name="liuyan" id="shijian" >
        </td>
    </tr>
    <tr>
        <td colspan="2" style="height:120px;">留言内容:
          <textarea name="content" cols="60" rows="5" onkeyup="countChar(this.value)" ></textarea>
            <label class="CountChar" id="CountChar"></label>
        </td>
    </tr>
</table>
    <input type="submit" name="jiao"  id="tijiao" value="提交"/>
</form>
<script type="text/javascript">
    /**
     * 检测ajax 对象
     */
    var xmlhttp,host;
     host='http://'+window.location.host;
     function getXmlhttp(){
         try{
             xmlhttp=new XMLHttpRequest(); //用户的浏览器符合 w3c 标准
         }catch (e){
             try{
                 xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); // ie7
             }catch(e){
                 try{
                     xmlhttp=new ActiveXObject("microsoft.XMLHTTP");// ie6 一下的
                 }catch(e){
                     alert("请更新你的浏览器!");
                 }
             }
         }
     }
     getXmlhttp();
   /**
     * 当页面加载时自动加载 runtime() 函数
     * 功能: 将动态时间添加到留言时间文本框中
     */
     function  runtime(){
         var date=new Date();//实例化对象 date
         var y=date.getFullYear(); //获取年
         var m=date.getMonth()+1; //获取月
         var d=date.getDate(); //获取日
         var h=date.getHours();//获取小时
         var minu=date.getMinutes();//获取分钟
         var sec=date.getSeconds();//获取秒
          h=h < 10?'0'+h:h; //前置加 0
         minu=minu < 10?'0'+minu:minu; //前置加 0
         sec=sec < 10 ? '0'+sec:sec; //前置加 0
        var timeStr=y+"/"+m+"/"+d+" "+h+":"+minu+":"+sec; //构建字符串
         document.getElementById('shijian').value=timeStr;
         setTimeout(runtime,1000);
     }
    /**
     * 检测title 是否包含非法字符
     * @param titleValue
     */
    function checkTitle(titleValue){
        if(titleValue){
            var url=host+"/message.php?title="+titleValue+"&"+Math.random(); //设置ajax的url
            xmlhttp.open("GET",url,true); //打开通信通道
            xmlhttp.onreadystatechange=callFunc;//设置回调函数
            xmlhttp.send(null);//客户端发送
        }else{
            var liubiaoInput=document.getElementById('liubiao'); //找到标题的input元素对象
            var tijiaoButt=document.getElementById('tijiao');  //找到提交按钮的button元素对象
            liubiaoInput.style.border='1px solid red'; //改变 标题的input元素的边框颜色为红色
            tijiaoButt.disabled=true;//提交按钮的button元素为不可用
         }
     }
    //实现回调函数
    function  callFunc(){
        if(xmlhttp.status == 200  && xmlhttp.readyState == 4){ //表示客户端准备好接收数据了
             var text=xmlhttp.responseText; //获取服务器的返回数据
            var liubiaoInput=document.getElementById('liubiao'); //找到标题的input元素对象
            var tijiaoButt=document.getElementById('tijiao');  //找到提交按钮的button元素对象
           if(text == '1'){ //判断服务器返回的数据 如果是 1 就改变 标题的input元素的边框,提交按钮的button元素为不可用
               liubiaoInput.style.border='1px solid red'; //改变 标题的input元素的边框颜色为红色
               tijiaoButt.disabled=true;//提交按钮的button元素为不可用
             }else{
                 liubiaoInput.style.border='1px solid #7F9DB9'; //标题的input元素的边框颜色为之前的本来颜色
                 tijiaoButt.disabled=false;//提交按钮的button元素为可用
             }
        }
    }
    /**
     * 统计用户输入了多少个字符
     */
    function countChar(text){
         var length=text.length;
         var lable=document.getElementById('CountChar');
        lable.innerHTML='你以输入了'+length+'个字符'
    }
</script>
</body>
</html>


服务器端:
  common.php --公共部分
 //非法字符
$errorKeyWord=array( 'tmd','他妈的','习近平','共产党',"混蛋","王八蛋","傻逼","笨蛋");
//相对于的替换字符
$replaceKeyWord=array('挺美的','挺美的','吸金票','工程队','婚典','晚八点','沙比','本队');


/**
 * 字符过滤
 */
function _addslash($str){
    if(get_magic_quotes_gpc()){
        return $str;
    } else{
        return  addslashes($str);
    }
}


/**
 * 替换内容中的非法字符函数
 */
function replaceKeyWord($content){
    global $errorKeyWord,$replaceKeyWord;
    return str_replace($errorKeyWord, $replaceKeyWord, $content);
}
-------------------


处理标题部分actionTitle.php
 require_once 'common.php'; //加载公共文件
/**
 * 检测标题逻辑
 */
 if(isset($_GET['title'])){
      $title=_addslash(trim($_GET['title']));
     if(checkTitle($title)){
        echo '1';
     }
}
/**
 * @param $str
 * @return bool
 * 检查标题函数
 */
function checkTitle($str){
    global $errorKeyWord;
    mb_internal_encoding("UTF-8"); //设置输入输出字符编码为 utf-8
    foreach($errorKeyWord as $key=>$keyword){
        $count=mb_substr_count($str,$keyword,'utf-8'); //字符统计 如果存在就返回 1 否则就返回 0
        $count=intval($count); //转换成int
        if($count != 0){
             return true;
         }
     }
    return false;
}


-----------
处理内容部分 actionMessage.php
require_once 'common.php';
/**
 * 处理表单提交
 */
if(isset($_POST['jiao'])){
    //将数据存放在数组中
    $data=array();
    //数据安全验证
    $data['title']=_addslash(trim($_POST['biaoti']));
    $data['time']=_addslash(trim($_POST['liuyan']));
    $content=_addslash(trim($_POST['content']));
    //检测非法字符,并替换,替换完成后返回新的字符串
    $data['content']=replaceKeyWord($content);
 }


?>
<!DOCTYPE html>
<html>
<head>
    <title>替换非法字符并输出</title>
</head>
<body>
<table border="1">
   <tr><th>标题</th><th>发表时间</th><th>发表内容</th></tr>
   <tr><td><?php echo $data['title']; ?></td><td><?php echo $data['time']; ?></td><td><?php echo $data['content']; ?></td></tr>
</table>
</body>
</html>

你可能感兴趣的:(PHP,String,前端)