开发者

Bad Word Filter, how to combine with URL Replace

I have two scripts - javascript and php..

this cleans the 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>

this is the bad word filter

<?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);
    }
    $sear开发者_高级运维ch=preg_replace("/\s+/"," ",$search);

    $keyword = str_replace(" ", "+", $search);
    }

    else
    {
    $keyword = str_replace(" ", "+a", $keyword);
    }
    ?> 

how can i combine this two scripts and replace the bad word in the url with "haha"?


You can redirect in the PHP

First, the form:

<form action="somefile.php">
<input type="text" id="search" name="search" value="" placeholder="Enter here..." />
<button>Search</button>
</form>

Second:

// 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");
  }

Or you can use ajax to check and clean the keyword:

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

You can look for more information about ajax / post / get (jQuery) in:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜