【问题标题】:Word filter with PHP使用 PHP 进行词过滤
【发布时间】:2017-06-18 13:13:56
【问题描述】:

我正在尝试在我的 Magento 网站中创建一个单词过滤器,基本上我有一个带有 textarea 和一个提交按钮的表单,如下所示:

<form id="answer_form_<?php echo $id;?>" class="form" method="post" 
action="<?php echo Mage::getUrl('productquestions/productquestions/saveanswers',array('product_questions_id'=>$id));?>">

<textarea id="txt_send" class="input-text required-entry " name="content" 
id="answer_content_<?php echo $id;?>" title="Content"></textarea>

<button id="btn_send" style="float: left;" type="submit" class="button" 
title="Send Message"><span><span><?php echo $this->__('Send Message') ?></span></span></button>

</form>

我需要做的是在表单提交之前它被保存在数据库中时从textarea过滤单词,所以我创建了一些php函数并对其进行了调整。最终代码为:

function wordFilter($text) {       
   $filter_terms = array('\bass(es|holes?)?\b','\bshit(e|ted|ting|ty|head)\b');
   $filtered_text = $text;

   foreach($filter_terms as $word) {
      $match_count = preg_match_all('/' . $word . '/i', $text, $matches);

      for($i = 0; $i < $match_count; $i++) {
         $bwstr = trim($matches[0][$i]);
         $filtered_text = preg_replace('/\b' . $bwstr . '\b/', str_repeat("*", strlen($bwstr)), $filtered_text);
     }

   }
   return $filtered_text;
}

if(isset($_POST['btn_send'])) {
   $text = htmlentities($_POST['txt_send']);

   $text = wordFilter($text);
}

到目前为止,我只添加了两个单词进行测试,当我使用这两个单词进行文本处理时,它会正常保存,而不会将它们更改为“*****”。我避免使用 JS,因为它是客户端。

谁能告诉我我错过了什么?

谢谢!

已编辑:

作为一个 Magento 插件。该操作将表单重定向到: productquestions/productquestions/saveanswers',array('product_questions_id'=&gt;$id)); 并根据 id 更改 url。例如:siteurl/index.php/productquestions/productquestions/saveanswers/product_questions_id/40 在此控制器页面中,我具有以下功能:

public function saveanswersAction() 
    {
        $answers = $this->getRequest()->getPost();
        $answerCollection = array();
        $model = Mage::getModel('productquestions/answers'); 
        $id = $this->getRequest()->getParam('product_questions_id');

        $model->setData('product_questions_id',$id);
        $model->setData('answers',$answers['content']);

        $model->save();
        $answerCollection[] = $model;

    }

【问题讨论】:

    标签: php forms function magento filter


    【解决方案1】:

    有点难以确定,因为您没有包含执行保存的代码,但看起来您没有将过滤器函数的结果保存回要保存的变量中。 试试:

    if(isset($_POST['btn_send'])) {
        $text = htmlentities($_POST['txt_send']);
        $text = wordFilter($text);
        //Code to save $text here
    }
    

    【讨论】:

      【解决方案2】:

      刚刚将代码移到控制器上,它可以工作,感谢您的提示。

      $text = $this->getRequest()->getParam('content');
              $filter_terms = array('\bass(es|holes?)?\b','\bshit(e|ted|ting|ty|head)\b');
              $filtered_text = $text;
              foreach($filter_terms as $word)
              {
                   $match_count = preg_match_all('/' . $word . '/i', $text, $matches);
                   for($i = 0; $i < $match_count; $i++)
                   {
                         $bwstr = trim($matches[0][$i]);
                         $filtered_text = preg_replace('/\b' . $bwstr . '\b/', str_repeat("*", strlen($bwstr)), $filtered_text);
                   }
              }
      
      $model->setData('answers',$filtered_text);
      

      【讨论】:

        猜你喜欢
        • 2018-03-11
        • 1970-01-01
        • 2012-10-23
        • 1970-01-01
        • 1970-01-01
        • 2013-03-09
        • 1970-01-01
        • 2019-11-24
        • 1970-01-01
        相关资源
        最近更新 更多