Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中转到文件的第一行?_Php_File - Fatal编程技术网

如何在php中转到文件的第一行?

如何在php中转到文件的第一行?,php,file,Php,File,我正在读取文件,如果搜索到的字符串匹配,则获取特定的行。要搜索的字符串串存储在数组中。我不能每次循环数组获取字符串时都打开文件。但是想转到文件的第一行并再次开始搜索。该文件包含大约15k行。如果我每次(在循环中)打开文件,它就可以正常工作。但是如果在循环之外打开文件。只返回第一个匹配的字符串行 $scheme_code = array("106212","112422","114239","104685","100122","118191","131666"); foreach($scheme

我正在读取文件,如果搜索到的字符串匹配,则获取特定的行。要搜索的字符串串存储在数组中。我不能每次循环数组获取字符串时都打开文件。但是想转到文件的第一行并再次开始搜索。该文件包含大约15k行。如果我每次(在循环中)打开文件,它就可以正常工作。但是如果在循环之外打开文件。只返回第一个匹配的字符串行

$scheme_code = 
array("106212","112422","114239","104685","100122","118191","131666");
foreach($scheme_code as $searchthis) {

        $handle = @fopen("myfile", "r"); 


        //DONT WANT TO DO THE ABOVE LINE FOR EVERY ITERATION

        if ($handle)
        {
            //echo "handle open"."<br>";
            while (!feof($handle))
            {

                $buffer = fgets($handle,4096);
                if(strpos($buffer, $searchthis) !== FALSE){
                    $matches[] = $buffer;
                }                       
            }               
        }       
    }
编辑-我尝试了倒带()。我收到通知“倒带():流不支持查找”

在这里,您可以使用函数,该函数将为您提供完整的行数组,然后您可以通过
fopen
逐行匹配,而无需每次使用IO资源

<?php

$linesArray = file("/path/to/your/file.txt"); 
foreach($linesArray as $line){
   // do the stuff or matching you want to perform line by line on $line
}

当我使用倒带时,请看一下,它说文件不支持查找。
<?php

$linesArray = file("/path/to/your/file.txt"); 
foreach($linesArray as $line){
   // do the stuff or matching you want to perform line by line on $line
}