Bad Word Filter,如何与 URL Replace 结合使用

发布于 2024-08-22 20:37:26 字数 829 浏览 5 评论 0原文

我有两个脚本 - 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 中的坏词替换为“哈哈”?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恏ㄋ傷疤忘ㄋ疼 2024-08-29 20:37:26

您可以在 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/

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/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文