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", "
", $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); }

你可能感兴趣的:(php)