百度
360搜索
搜狗搜索

php逐行读取.txt文件内容,并解析每行内容详细介绍

  php逐行读取.txt文件内容,并解析每行内容的方法:

    // 读取nlp text 并存到mongodb
    public function readNLP(&$errorCode,&$errorMessage)
    {
        try{
            // $_SERVER["DOCUMENT_ROOT"],获取当前运行脚本所在文档根目录。$filePath为.txt文件所在路径
            $filePath = $_SERVER["DOCUMENT_ROOT"] . "/checkdata/app/files/nlp.txt";
            $file = fopen($filePath, "r"); // 以只读的方式打开文件
            if(empty($file)){
                $errorCode = 201;
                $errorMessage = "file not found";
                return;
            }
            $i = 0;
            //输出文本中所有的行,直到文件结束为止。
            while(!feof($file)) {
                $itemStr = fgets($file); //fgets()函数从文件指针中读取一行
                $itemArray = explode("\t",$itemStr); // 将tab分割的各部分内容提取出来
                $itemArray = array_filter($itemArray); // 对itemArray进行校验
                $textDB = new Text();
                if($textDB->findItemByText($itemArray[3]) === false){ // 数据库中不存在该item,insert
                    $addResult = $textDB->addNewItem($itemArray[3],$itemArray[4]);
                    if($addResult === false){
                        $errorCode = 201;
                        $errorMessage = "insert new item failed";
                        return "currentIndex: " . $i . " , " . $itemArray[3];
                    }
                }
                ++$i;
            }
            fclose($file);
        }catch (Exception $exception){
            $errorCode = $exception->getCode();
            $errorMessage = $exception->getMessage();
        }
        return true;
    }

阅读更多 >>>  remove函数用法python,python里remove函数

网站数据信息

"php逐行读取.txt文件内容,并解析每行内容"浏览人数已经达到31次,如你需要查询该站的相关权重信息,可以点击进入"Chinaz数据" 查询。更多网站价值评估因素如:php逐行读取.txt文件内容,并解析每行内容的访问速度、搜索引擎收录以及索引量、用户体验等。 要评估一个站的价值,最主要还是需要根据您自身的需求,如网站IP、PV、跳出率等!