Home > Backend Development > PHP Tutorial > php关键词拦截程序,拦截后如何知道哪个词为非法词组

php关键词拦截程序,拦截后如何知道哪个词为非法词组

WBOY
Release: 2016-06-13 10:23:45
Original
1004 people have browsed it

php关键词拦截程序,拦截后怎么知道哪个词为非法词组?
一、panduan.php

/**
 * 判断文本中是否存在关键词
 */
function filter_text($text, $badstring){
$filter = explode('|', $badstring);
foreach ($filter as $key) {
if(strpos($text, $key) !== false){
return true;
}
}
return false;
}

?>

二、filter.php
$badstring="拦我|此内容|不允许";
?>

三、index.php
require_once("panduan.php");
require_once("filter.php");
$biaoti="如果包含此内容就不能通过!";
if(filter_text($biaoti, $badstring)){
echo "对不起,你提示的内容包含“$*****”文字不能表,请删除“此内容”文字后再提交!";
exit();
}
?>



以上代码能正确拦截事先设置好的关键词,但怎么写才能提示是哪个关键词非法的?如果取得引号中的内容:“$*****”?

请高手帮忙,谢谢!

------解决方案--------------------
在这里修改成
 if(strpos($text, $key) !== false){
return $key;
}

这里
if(filter_text($biaoti, $badstring)){
echo "对不起,你提示的内容包含“$*****”文字不能表,请删除“此内容”文字后再提交!";
exit();
}

修改成

$badstr=filter_text($biaoti, $badstring);

if($badstr){
echo "对不起,你提示的内容包含“.$badstr.”文字不能表,请删除“此内容”文字后再提交!";
exit();
}

大约如此.
------解决方案--------------------

PHP code
$text = '拦我';$badstring = "/拦我|此内容|不允许/";if (preg_match($badstring, $text, $matchs)){    echo $matchs[0]; //var_dump($matchs);}<br><font color="#e78608">------解决方案--------------------</font><br><strong>关于二者的效率</strong><br><br>测试代码:<br>
Copy after login
PHP code
$start = microtime(true);$text = '拦我';$badstring = "/拦我|此内容|不允许/";$count = 0;for($i = 0; $i 
                 
              
              
        
            
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template