开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜