0%

PHP 读取文件指定行

以下的函数可以根所需要读取目标地址第N行的内容

<?php
$url="http://www.fwvga.com/";//地址
$line=1;//读取第几行,从1开始
echo getline($url,$line);
function getline($url,$line){
if($line<1)return "error";
$handle = @fopen($url, "r");
$i=0;
if ($handle) {
    while (!feof($handle)) {
        $buffer = fgets($handle, 4096);
        if($i==$line-1){
        fclose($handle);
        return $buffer;		
        }
        $i++;
    }
    fclose($handle);
    return  "error";
}
}
?>