replaceChar = $replaceChar; $this->fuzzyMatch = $fuzzyMatch; $this->setWords($this->txtDir); } public function setWords($dir) { $txtArr = $this->readTxtFiles($dir); $wordArr = explode(',', implode(',', $txtArr)); foreach ($wordArr as $k => $v) { if (empty(trim($v))) { unset($wordArr[$k]); } } $this->words = array_chunk($wordArr, 100); } public function getWords() { $dir = $this->txtDir; $txtArr = $this->readTxtFiles($dir); $wordArr = explode(',', implode(',', $txtArr)); // foreach ($wordArr as $k => $v) { // if (empty(trim($v))) { // unset($wordArr[$k]); // } // } $text = implode(',', $wordArr); return $text; } /** * @param $folderPath * @return array * 遍历读取文件夹下txt内容 */ private function readTxtFiles($folderPath) { $arr = []; if (is_dir($folderPath)) { $files = scandir($folderPath); foreach ($files as $file) { $filePath = $folderPath . '/' . $file; if (is_file($filePath) && pathinfo($filePath, PATHINFO_EXTENSION) === 'php') { $content = file_get_contents($filePath); $content = str_replace(['words as $arr) { foreach($arr as $word) { $star = str_repeat('*', mb_strlen($word, 'utf-8')); $result = str_replace($word, $star, $result); } } return $result; } }