0
votes

Did CakePHP have the function to find and replace word by word in a script?

I have the database to contain the "bad words" and "replace words".

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

I have the view allowed user to input script into textarea. When I push the submit, all the bad word in the script will be replaced with replace word from the database.

how can I find the bad words in the script and replace it as the database contents.

Thank for all view and suggestion.

EDIT: Nov 18, 2014

Here is my controller

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

The data show will be like in view:

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

The word_check() is the function to check the script.

The echo $word_item['MAvoidanceWord']['avoidance_word']; will be array of bad word content

The echo $word_item['MAvoidanceWord']['replace_word']; will be array of replace word content

I get the array of bad word data to check and replace word to filter already. How I just want to check word by word in the script which I submit from the Form and replace it.

I tried to use preg_replace($badWords , $replaceWords , $userText ); but it break the UTF-8 coding. The "str_replace" is same.

EDIT: Nov 19, 2014 PROBLEM SOVLED

Now I found the way to solve the problems. Here is final code. Hope this will help more people in this situation.

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
Can you give your expected output? - Sadikhasan
OK, Sth like: we will type the content as script into textarea. After push Sumbit button, the system will check the "bad words"'s list in the database and replace (if exist "bad words") in script and show it on the view again. - TommyDo

3 Answers

1
votes

This may not be a good solution, but you can display all text and than use str_replace() to change all the "bad" words to "good" words. With a while loop on your badwords.

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

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

Hope this helps :)

1
votes

General process:

  1. Pulling all the bad words and the replacement words
  2. Putting them into sorted arrays, respectively (probably using their IDs)
  3. Replacing the "bad words" using preg_replace

Code Sample:

$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 );
0
votes

For replacing the words there is a function in String utility but the checks have to be done manually.