您现在的位置是:首页 > 其他

李清波 2017-11-10 其他 3808 复制当前网址

PHP获取文件中指定行数的数据


PHP获取文件中指定行数的数据


/**
 * 获取指定行内容
 *
 * @param $file 文件路径
 * @param $line 行数
 * @param $length 指定行返回内容长度
 */
function get_line($file, $line, $length = 1000){
    $returnTxt = null; // 初始化返回
    $i = 1; // 行数

    $handle = @fopen($file, "r");
    if ($handle) {
        while (!feof($handle)) {
            $buffer = fgets($handle, $length);
            if($line == $i) $returnTxt = $buffer;
            $i++;
        }
        fclose($handle);
    }
    return $returnTxt;
}


文章来源:http://liqingbo.com/blog-1292.html

评论