如何在.txt php文件中放置数组坏字

如何在.txt php文件中放置数组坏字,php,Php,我试图用这段代码来过滤不好的单词,效果很好,但我想把不好的单词放在.txt文件中,我尝试了不同的方法,但没有成功 $GLOBALS['bad_words']= array('truck' => true, 'shot' => true); function containsBadWord($str){ $str= trim($str); $str= preg_replace('/\s+/', ' ', $str); $word_list= explode("

我试图用这段代码来过滤不好的单词,效果很好,但我想把不好的单词放在.txt文件中,我尝试了不同的方法,但没有成功

$GLOBALS['bad_words']= array('truck' => true, 'shot' => true);

function containsBadWord($str){
    $str= trim($str);
    $str= preg_replace('/\s+/', ' ', $str);
    $word_list= explode(" ", $str);
    foreach($word_list as $word){
        if( isset($GLOBALS['bad_words'][$word]) ){
            return true;
        }
    }
    return false;
}

要将坏字写入文件,可以使用并将其存储在JSON中


我没有看到任何写入文件的代码
array('truck'=>true,'shot'=>true)中的坏字我想要卡车射击等txt文件你说我尝试不同的方式。那么,你到底尝试了什么?您对尝试的结果有何期待?我尝试了
文件('/path/to/bad_word.txt')
file\u获取内容
,为了使其成为
json
,它无法进行过滤。希望您的用户都不是来自。谢谢,但我正在尝试从bad_word.txt获取truck word和shot,如下所示
$bad_word=file_get_contents('/path/to/bad_word.txt')
$GLOBALS['bad\u words']=$bad\u word@Destroyer添加了一个更新。但是,您是否要继续在文件中读写?还是静止的`
<?php

$GLOBALS['bad_words']= array('truck' => true, 'shot' => true);

file_put_contents('/path/to/bad_word.txt',json_encode($GLOBALS['bad_words']));
$bad_words = json_decode(file_get_contents('/path/to/bad_word.txt'),true);
$GLOBALS['bad_words'] = $bad_words;