CakePHP 1.3:在脚本中进行查找和替换单词功能

时间:2014-11-17 09:10:03

标签: php cakephp

CakePHP是否具有在脚本中逐字查找和替换的功能?

我有数据库包含"坏词"和"替换单词"。

ID | Bad Words | Replace Words
1  |    ABC    |     ABC1
2  |    EDF    |     EDF1
3  |    GHI    |     GHI1

我有视图允许用户将脚本输入textarea。当我按下提交时,脚本中的所有坏词都将被替换为数据库中的替换词。

如何在脚本中找到坏词并将其替换为数据库内容。

感谢所有观点和建议。

编辑:2014年11月18日

这是我的控制器

    class MAvoidanceWordsController extends AppController{

    //The function to check bad words in script
    function word_check()
    {
        //If the submit button (name is "check") is pushed.
        if(isset($_POST['check']))
        {
            //Get all value in form
            $script_content = $this->data;
            //Query in database
            $avoidance_list = $this->MAvoidanceWord->find('all');
            //Get Avoidance word
            $this->set("word_data",$word_data);
            foreach ($word_data as $word_item):
                echo $word_item['MAvoidanceWord']['avoidance_word'];
                echo "-----------";
                echo $word_item['MAvoidanceWord']['replace_word'];
                echo "<br/>";
            endforeach;
        }
    }

数据显示将如下:

嬢-----------手
看護婦-----------看護師
スチュワーデス-----------フライトアテンダント
百姓-----------農民
ブラインドタッチ-----------タッチタイピング
保母-----------保育士
ヤンキー-----------アメリカ人

word_check()是检查脚本的功能。

echo $ word_item [&#39; MAvoidanceWord&#39;] [&#39; avoidance_word&#39;]; 将是坏词内容数组

echo $ word_item [&#39; MAvoidanceWord&#39;] [&#39; replace_word&#39;]; 将是替换字词内容的数组

我得到坏字数据数组来检查并替换已经过滤的字。我只想在我从表单中提交的脚本中逐字检查并替换它。

我尝试使用 preg_replace($ badWords,$ replaceWords,$ userText); 但它打破了UTF-8编码。 &#34; str_replace &#34;是一样的。

编辑:2014年11月19日问题出现

现在我找到了解决问题的方法。这是最终的代码。希望这会在这种情况下帮助更多的人。

function word_check()
        {
            //Read data from textarea on from
            if(isset($_POST['check']))
            {
                $script_content = $this->data['MAvoidanceWord']['textarea'];
                $word_data = $this->MAvoidanceWord->find('all',array(
                    'fields' => 'avoidance_word,replace_word',
                    'conditions' => array('invalid_flg' => '0')
                ));

                foreach ($word_data as $word_item):
                    $bad[] = $word_item['MAvoidanceWord']['avoidance_word'];
                    $good[] = $word_item['MAvoidanceWord']['replace_word'];
                endforeach;

                $result = str_replace($bad, $good, $script_content);
                $this->set('view_result',$result);
            }
}

3 个答案:

答案 0 :(得分:1)

这可能不是一个好的解决方案,但您可以显示所有文字,而不是使用str_replace()将所有“坏”字改为“好”字。在你的坏词上有一个while循环。

$sql = 'SELECT * FROM `table`';

while($result of sql){
    str_replace($result['badword'],$result['goodword'],$texttosearch);
}

希望这会有所帮助:)

答案 1 :(得分:1)

一般过程:

  1. 拉出所有坏词和替换词
  2. 分别将它们放入已排序的数组中(可能使用它们的ID)
  3. 取代&#34;坏词&#34;使用preg_replace
  4. 代码示例:

    $userText = $_POST['userinput'];
    $badWords = array(1=>'ABC', 2=>'EDF',3=>'GHI');
    $replaceWords = array(1=>'ABC1', 2=>'EDF2',3=>'GHI2');
    $filteredText = preg_replace($badWords , $replaceWords , $userText );
    

答案 2 :(得分:0)

要替换单词,String实用程序中有一个功能,但必须手动完成检查。