编程爱好者之家

PHP获取文件中指定行数的内容

2018-07-22 11:29:12 338

/**
 * 获取指定行内容
 *
 * @param $filePath文件路径
 * @param $line 行数
 * @param $length 指定行返回内容长度
 */
function getContent($filePath, $lineNum, $length = 2000){
    $result = null; // 初始化返回
    $i = 1; // 行数
 
    $handle = @fopen($filePath, "r");
    if ($handle) {
        while (!feof($handle)) {
            $buffer = fgets($handle, $length);
            if($lineNum== $i) $result = $buffer;
            $i++;
        }
        fclose($handle);
    }
    return $result ;
}


同类文章