123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?php
- namespace ins;
- class words
- {
- private $words;
- private $replaceChar;
- private $fuzzyMatch;
- private $txtDir = IA_ROOT_WK.'/extend/ins/text';
- public function __construct($replaceChar = '*', $fuzzyMatch = false)
- {
- $this->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(['<?php', '/*', '*/'], '', $content);
- $content = trim($content);
- $arr[$file] = $content;
- }
- }
- }
- return $arr;
- }
- public function filter($text)
- {
- $result = $text;
- foreach($this->words as $arr) {
- foreach($arr as $word) {
- $star = str_repeat('*', mb_strlen($word, 'utf-8'));
- $result = str_replace($word, $star, $result);
- }
- }
- return $result;
- }
- }
|