坏词过滤器,如何与URL替换相结合

时间:2010-02-25 00:25:48

标签: php jquery filter replace word

我有两个脚本--javascript和php ..

清理网址

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

我如何结合这两个脚本并用“haha”替换网址中的坏词?

1 个答案:

答案 0 :(得分:1)

您可以在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 / post / get(jQuery)的更多信息:

http://api.jquery.com/jquery.ajax/
http://api.jquery.com/jquery.post/
http://api.jquery.com/jquery.get/