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