刪除一些話我有這樣的字符串:解析字符串 - 從字符串
$text = 'Hello this is my string and texts';
我有一些不允許在陣列話:
$filtered_words = array(
'string',
'text'
);
我要全部更換我在$text
與***
過濾詞,所以我寫道:
$text_array = explode(' ', $text);
foreach($text_array as $key => $value){
if(in_array($text_array[$key], $filtered_words)){
$text = str_replace($text_array[$key], '***', $text);
}
}
echo $text;
輸出:
Hello this is my *** and texts
但我需要的功能也與***
取代texts
,因爲它也包含過濾詞(文本)。
我怎麼能做到這一點?
由於
http://php.net/str_replace - 搜索*數組*,它的工作原理。替換字符串是'***' - 只需檢查手動PHP是否開箱即用 - 請參閱[答案](http://stackoverflow.com/a/14919021/367456) – hakre 2013-02-17 07:43:13