Aug
28
2013
php指定行读取文件的方法
/*
* $file 文件
* $start 1 从首行开始,0 随机取,-1 从尾行开始
* 结果数组
/*
function getFileRows($file, $start = 1, $num = 0) {
$_start = $start;
$rowsdata = array ();
$lines = file ( $file );
if ($_start > 0) {
$start = $start - 1;
} else {
$start = count ( $lines ) + $start;
}
$num = $num == 0 ? count ( $lines ) - $start : $num;
for($i = 0; $i < $num; $i ++) {
if ($_start == 0) {
$line = trim ( $lines [array_rand ( $lines )] );
if (in_array ( $line, $rowsdata )) {
$line = trim ( $lines [array_rand ( $lines )] );
}
$rowsdata [] = $line;
} else {
if ($_start > 0) {
$k = $start + $i;
} else {
$k = $start - $i;
}
$rowsdata [] = trim ( $lines [$k] );
}
}
return $rowsdata;
}
var_dump ( getFileRows ( './Inc/proxy.txt', 0, 10 ) );
微信扫一扫,打赏作者吧~
最活跃的读者