php 讀取txt文件 匹配數據

    public function readWorld()
    {

        $path = public_path() . "test.txt";
        //要匹配的字段
        $cloumn = array("Start", "Finish", "Scan", "Server", "Scan");

        $str = file_get_contents($path);//將整個文件內容讀入到一個字符串中
//        $str = str_replace("\r\n", "<br />", $str);
        $array = explode("\r\n", $str);
        $arr = array();

        foreach ($cloumn as $pe) {

            foreach ($array as $line => $content) {
                echo 'line ' . ($line + 1) . ':' . $content;
                if (strstr($content, $pe)) {
                    $arr[str_replace(' ', '', $pe)] = trim(str_replace($pe, '', $content));
//                    echo 'line ' . ($line + 1) . ':' . $content;
                }
            }
        }
        dd($arr);

    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章