CakePHP是否具有在腳本中逐字查找和替換的功能?CakePHP 1.3:在腳本中查找和替換字詞功能
我有數據庫包含「壞詞」和「替換詞」。
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 ['MAvoidanceWord'] ['avoidance_word'];將壞字內容列表
echo $ word_item ['MAvoidanceWord'] ['replace_word'];將是替換單詞內容的數組
我得到壞字數據的數組來檢查並替換已經過濾的單詞。我如何只想在我從表單提交的腳本中逐字檢查並替換它。
我試着用preg_replace($ badWords,$ replaceWords,$ userText);但它打破了UTF-8編碼。 「str_replace」是相同的。
編輯:2014年11月19日問題SOVLED
現在我發現解決問題的方法。這是最終的代碼。希望這會在這種情況下幫助更多的人。
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);
}
}
你可以給你預期的輸出? – Sadikhasan
好吧,就像:我們將內容作爲腳本輸入到textarea中。按下Sumbit按鈕後,系統會檢查數據庫中的「壞詞」列表,並在腳本中替換(如果存在「壞詞」)並再次顯示在視圖中。 – TommyDo