本文讲述如何使用PHP和Ajax创建一个过滤敏感词汇的方法,判断是否有敏感词汇。
敏感词汇数组sensitive.php
1 return array ( 2 0 => '111111', 3 1 => 'aaaaaa', 4 2 => 'bbbbbb', 5 3 => 'ccccc', 6 4 => 'dddd', 7 5 => 'eeee', 8 ........ 9 )
判断是否有敏感词汇
1 $("#add").click(function() { 2 var content = $("#content").val(); 3 $.ajax({ 4 type: "POST", 5 url: "ajax.php", 6 data: "content=" + content, 7 success: function(data) { 8 if (data) { 9 $("#message").html(data).show(); 10 } else { 11 $("#message").html('没有敏感词汇').show(); 12 } 13 } 14 }); 15 });
PHP判断数组里是否有敏感词汇,若是有则输出
1 $content = htmlspecialchars($_POST['content']); 2 $arr= include 'sensitive.php'; 3 foreach ($arr as $v) { 4 if (false !== strstr($content, $v)){ 5 echo "含有敏感词汇 ".$v; 6 exit; 7 } 8 }