开发者

Script to read in line, select from value, and print to file

开发者 https://www.devze.com 2023-04-09 16:21 出处:网络
So I have a php script, I want to read in a file line by line, each line only contains one id.I want to select using sql for each id in the file, then print the result for each selection in the same f

So I have a php script, I want to read in a file line by line, each line only contains one id. I want to select using sql for each id in the file, then print the result for each selection in the same file.

so far i have:

    while (!feof($file))
{
    // Get the current line that the file is reading
    $cur开发者_高级运维rentLine = fgets($file) ;
    //explodes integers by amount of sequential spaces
    //$currentLine = preg_split('/[\s,]+/', $currentLine);
    echo $currentLine;  //this echo statement prints each line correctly
    selectQuery($currentLine) ;


}   

fclose($file) ;

as a test so far i only have

 function selectQuery($currentLine){
     echo $currentLine;  //this is undefined?
 }


The result of fgets is never undefined. However, your approach is way too low-level. Use file and array_filter:

$results = array_filter(file('input.filename'), function(line) {
  return strpos($line, '4') !== false; // Add filter here
});
var_export($results); // Do something with the results here
0

精彩评论

暂无评论...
验证码 换一张
取 消