坏词过滤器,如何与URL结合替换
问题描述:
我有两个脚本 - JavaScript和PHP ..坏词过滤器,如何与URL结合替换
这种清洁网址
<script type="text/javascript">
$(document).ready(function() {
$('.search-form').submit(function() {
window.location.href = "/file_"+ $('.search-form input:text').val() + ".html";
return false;
});
});
</script>
这是坏词过滤器
<?php
if (isset($_GET['search']))
{
$search=$_GET['search'];
if(is_array($badwords) && sizeof($badwords) >0)
{
foreach($badwords as $theword)
$search = ereg_replace($theword,"haha",$search);
}
$search=preg_replace("/\s+/"," ",$search);
$keyword = str_replace(" ", "+", $search);
}
else
{
$keyword = str_replace(" ", "+a", $keyword);
}
?>
我该如何结合这两个脚本,并用“哈哈”替换url中的坏词?
答
您可以在PHP
首先重定向,形式:
<form action="somefile.php">
<input type="text" id="search" name="search" value="" placeholder="Enter here..." />
<button>Search</button>
</form>
二:
// somefile.php
if (isset($_GET['search'])){
$search=$_GET['search'];
if(count($badwords)){
foreach($badwords as $theword)
$search = ereg_replace($theword,"haha",$search);
}
$search=preg_replace("/\s+/"," ",$search);
$keyword = str_replace(" ", "+", $search);
} else {
$keyword = str_replace(" ", "+a", $keyword);
}
// here you can do any checks with the search and redirect to anywhere
if (strlen($keyword)){
header("location: /file_{$keyword}.html");
}
或者你可以使用AJAX来检查和清理关键字:
<script type="text/javascript">
$(document).ready(function() {
$('.search-form').submit(function() {
$.ajax({ type: "POST", dataType: "HTML",
url: "clean.php",
data: { search: $('.search-form input:text').val()},
success: function(response){
if (response.length > 0) {
window.location.href = "/" + response;
}
}
});
</script>
Clean.php:
if (isset($_GET['search'])){
$search=$_GET['search'];
if(count($badwords)){
foreach($badwords as $theword)
$search = ereg_replace($theword,"haha",$search);
}
$search=preg_replace("/\s+/"," ",$search);
$keyword = str_replace(" ", "+", $search);
} else {
$keyword = str_replace(" ", "+a", $keyword);
}
// here you can do any checks with the search and redirect to anywhere
if (strlen($keyword)){
echo("file_{$keyword}.html");
} ?>
你可以看看关于Ajax的更多信息/后/获取(jQuery的):
http://api.jquery.com/jquery.ajax/
http://api.jquery.com/jquery.post/
http://api.jquery.com/jquery.get/
只是一个谨慎的词:脏话过滤器通常是一个坏主意,因为他们往往充斥着腐败的有效用户和那些试图绕过他们的人可以简单地转向1337说话并使用特殊字符来传递信息。只有两个例子:assignment和ashita.org,因为它们都包含频繁被标记的词,你会得到像hahaignment和ahahaa.org的东西。 – 2010-02-25 00:35:20
等到有人问笔岛:O – 2010-02-25 00:41:54