本文整理汇总了PHP中SplFileObject::next方法的典型用法代码示例。如果您正苦于以下问题:PHP SplFileObject::next方法的具体用法?PHP SplFileObject::next怎么用?PHP SplFileObject::next使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SplFileObject
的用法示例。
在下文中一共展示了SplFileObject::next方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: readLine
/**
* @inheritDoc
*/
public function readLine()
{
if (!$this->file->eof()) {
$line = $this->file->current();
$this->file->next();
return json_decode($line);
}
return false;
}
示例2: readFileByLines
/**
* 返回文件从X行到Y行的内容(支持php5、php4)
* @param String $filename 文件名
* @param integer $startLine 开始行
* @param integer $endLine 结束行
* @param string $method 方法
* @return array() 返回数组
*/
function readFileByLines($filename, $startLine = 1, $endLine = 50, $method = 'rb')
{
$content = array();
$count = $endLine - $startLine;
// 判断php版本(因为要用到SplFileObject,PHP>=5.1.0)
if (version_compare(PHP_VERSION, '5.1.0', '>=')) {
$fp = new SplFileObject($filename, $method);
// 转到第N行, seek方法参数从0开始计数
$fp->seek($startLine - 1);
for ($i = 0; $i <= $count; ++$i) {
// current()获取当前行内容
$content[] = $fp->current();
// 下一行
$fp->next();
}
} else {
//PHP<5.1
$fp = fopen($filename, $method);
if (!$fp) {
return 'error:can not read file';
}
// 跳过前$startLine行
for ($i = 1; $i < $startLine; ++$i) {
fgets($fp);
}
// 读取文件行内容
for ($i; $i <= $endLine; ++$i) {
$content[] = fgets($fp);
}
fclose($fp);
}
// array_filter过滤:false,null,''
return array_filter($content);
}
示例3: getCode
/**
* Get Code from file
* @param $path
* @param $line
* @param $numLines
* @return array|null
*/
private function getCode($path, $line, $numLines)
{
if (empty($path) || empty($line) || !file_exists($path)) {
return NULL;
}
try {
// Get the number of lines in the file
$file = new \SplFileObject($path);
$file->seek(PHP_INT_MAX);
$totalLines = $file->key() + 1;
// Work out which lines we should fetch
$start = max($line - floor($numLines / 2), 1);
$end = $start + ($numLines - 1);
if ($end > $totalLines) {
$end = $totalLines;
$start = max($end - ($numLines - 1), 1);
}
// Get the code for this range
$code = array();
$file->seek($start - 1);
while ($file->key() < $end) {
$code[$file->key() + 1] = rtrim($file->current());
$file->next();
}
return $code;
} catch (RuntimeException $ex) {
return null;
}
}
示例4: export
protected function export($var, $return = false)
{
if ($var instanceof Closure) {
/* dump anonymous function in to plain code.*/
$ref = new ReflectionFunction($var);
$file = new SplFileObject($ref->getFileName());
$file->seek($ref->getStartLine() - 1);
$result = '';
while ($file->key() < $ref->getEndLine()) {
$result .= $file->current();
$file->next();
}
$begin = strpos($result, 'function');
$end = strrpos($result, '}');
$result = substr($result, $begin, $end - $begin + 1);
} elseif (is_object($var)) {
/* dump object with construct function. */
$result = 'new ' . get_class($var) . '(' . $this->export(get_object_vars($var), true) . ')';
} elseif (is_array($var)) {
/* dump array in plain array.*/
$array = array();
foreach ($var as $k => $v) {
$array[] = var_export($k, true) . ' => ' . $this->export($v, true);
}
$result = 'array(' . implode(', ', $array) . ')';
} else {
$result = var_export($var, true);
}
if (!$return) {
print $result;
}
return $result;
}
示例5: prepare
/**
* Write the compiled csv to disk and return the file name
*
* @param array $sortedHeaders An array of sorted headers
*
* @return string The csv filename where the data was written
*/
public function prepare($cacheFile, $sortedHeaders)
{
$csvFile = $cacheFile . '.csv';
$handle = fopen($csvFile, 'w');
// Generate a csv version of the multi-row headers to write to disk
$headerRows = [[], []];
foreach ($sortedHeaders as $idx => $header) {
if (!is_array($header)) {
$headerRows[0][] = $header;
$headerRows[1][] = '';
} else {
foreach ($header as $headerName => $subHeaders) {
$headerRows[0][] = $headerName;
$headerRows[1] = array_merge($headerRows[1], $subHeaders);
if (count($subHeaders) > 1) {
/**
* We need to insert empty cells for the first row of headers to account for the second row
* this acts as a faux horizontal cell merge in a csv file
* | Header 1 | <---- 2 extra cells ----> |
* | Sub 1 | Subheader 2 | Subheader 3 |
*/
$headerRows[0] = array_merge($headerRows[0], array_fill(0, count($subHeaders) - 1, ''));
}
}
}
}
fputcsv($handle, $headerRows[0]);
fputcsv($handle, $headerRows[1]);
// TODO: Track memory usage
$file = new \SplFileObject($cacheFile);
while (!$file->eof()) {
$csvRow = [];
$row = json_decode($file->current(), true);
if (!is_array($row)) {
// Invalid json data -- don't process this row
continue;
}
foreach ($sortedHeaders as $idx => $header) {
if (!is_array($header)) {
$csvRow[] = isset($row[$header]) ? $row[$header] : '';
} else {
// Multi-row header, so we need to set all values
$nestedHeaderName = array_keys($header)[0];
$nestedHeaders = $header[$nestedHeaderName];
foreach ($nestedHeaders as $nestedHeader) {
$csvRow[] = isset($row[$nestedHeaderName][$nestedHeader]) ? $row[$nestedHeaderName][$nestedHeader] : '';
}
}
}
fputcsv($handle, $csvRow);
$file->next();
}
$file = null;
// Get rid of the file handle that SplFileObject has on cache file
unlink($cacheFile);
fclose($handle);
return $csvFile;
}
示例6: readFile
/**
* @param \SplFileObject $stream
*
* Reads the file to parse its lines
*/
public function readFile(\SplFileObject $stream)
{
$stream->setFlags(\SplFileObject::READ_CSV | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY | \SplFileObject::DROP_NEW_LINE);
while (!$stream->eof()) {
$this->parseLine($stream->current());
$stream->next();
}
$stream = null;
}
示例7: actionIndex
public function actionIndex()
{
Yii::import("application.models.*");
//get the latest idle cron job
/* @var $latestidle JobQueue*/
$latestidle = JobQueue::model()->findByAttributes(array("status" => JobQueue::$JOBQUEUE_STATUS_IDLE));
if (!$latestidle) {
echo "No file queued";
die;
}
//set status to on-going
$latestidle->status = JobQueue::$JOBQUEUE_STATUS_ON_GOING;
$latestidle->save(false);
//retrieve file
$queueFile = new SplFileObject($latestidle->filename);
//read file
$queueFile->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::DROP_NEW_LINE | SplFileObject::SKIP_EMPTY);
$queueFile->next();
// Total_records ,
$queueFile->seek(PHP_INT_MAX);
$linesTotal = $queueFile->key();
$latestidle->total_records = $linesTotal;
$latestidle->save(false);
$index = 0;
foreach ($queueFile as $currentLine) {
//iterate content
if ($queueFile->key() === 0) {
continue;
}
//TODO: processed_record
$latestidle->processed_record = ++$index;
$latestidle->save(false);
$currentMobile = $currentLine[0];
$newBlackListedmobile = new BlackListedMobile();
//cleaning time
$currentMobile = trim($currentMobile);
$currentMobile = rtrim($currentMobile);
$currentMobile = ltrim($currentMobile);
$newBlackListedmobile->mobile_number = $currentMobile;
$newBlackListedmobile->queue_id = $latestidle->queue_id;
//set queueid
if ($newBlackListedmobile->save()) {
//save content
echo "{$newBlackListedmobile->mobile_number} : Saved \n";
} else {
echo "{$newBlackListedmobile->mobile_number} : Failed \n";
}
}
//when done
//set status to done
$latestidle->status = JobQueue::$JOBQUEUE_STATUS_DONE;
$latestidle->date_done = date("Y-m-d H:i:s");
//set done datetime to now()
$latestidle->save();
echo "Queue DONE \n";
}
示例8: rewind
public function rewind()
{
parent::rewind();
// If we are getting the first line as columns, take them then skip to
// the next line.
if ($this->_firstLineIsColumns) {
$this->setColumns(parent::current());
parent::next();
}
}
示例9: generate
function generate($nb, $fixture = __DIR__ . '/fixtures/lorem.txt')
{
$lorems = new SplFileObject($fixture, 'a+');
$txt = '';
while ($nb > 0 && $lorems->valid()) {
$txt .= $lorems->current();
$lorems->next();
$nb--;
}
return $txt;
}
示例10: getContents
/**
* @param $fileName
* @param $page
* @param $rows
* @param string $methord
* @return array
*/
public static function getContents($fileName, $page, $rows, $methord = 'rb')
{
$content = array();
$start = ($page - 1) * $rows;
$fp = new \SplFileObject($fileName, $methord);
$fp->seek($start);
for ($i = 0; $i < $rows; ++$i) {
$content[] = $fp->current();
$fp->next();
}
return array_filter($content);
}
示例11: getData
public function getData()
{
try {
$file = new SplFileObject($this->tmp_name, 'rb');
$data = null;
for ($file; $file->valid(); $file->next()) {
$data .= $file->current();
}
return $data;
} catch (RuntimeException $e) {
}
return null;
}
示例12: testCheckAnswerTask3
public function testCheckAnswerTask3()
{
$this->assertFileExists('data/export.csv');
$filecsv = new \SplFileObject('data/export.csv');
$filecsv->setFlags(\SplFileObject::READ_CSV);
$header = $filecsv->current();
$processor = new YieldProcessor(ReaderFactory::create('Db\\Product', include 'config/database.php'), WriterFactory::create('Stub'), function () {
});
foreach ($processor->processing() as $item) {
$filecsv->next();
$this->assertEquals($item, array_combine($header, $filecsv->current()));
}
}
示例13: next
/**
* Move forward to next element
*/
public function next()
{
if (null === $this->_file) {
$this->_openFile();
}
if ($this->_file) {
$this->_key = $this->_key + 1;
if (!$this->_file->eof()) {
$this->_file->next();
$this->_filepos = $this->_file->ftell();
}
}
}
示例14: testAgainstOriginalSedImplementation
public function testAgainstOriginalSedImplementation()
{
$tokenizer = new PennTreeBankTokenizer();
$tokenized = new \SplFileObject(TEST_DATA_DIR . "/Tokenizers/PennTreeBankTokenizerTest/tokenized");
$tokenized->setFlags(\SplFileObject::DROP_NEW_LINE);
$sentences = new \SplFileObject(TEST_DATA_DIR . "/Tokenizers/PennTreeBankTokenizerTest/test.txt");
$sentences->setFlags(\SplFileObject::DROP_NEW_LINE);
$tokenized->rewind();
foreach ($sentences as $sentence) {
if ($sentence) {
$this->assertEquals($tokenized->current(), implode(" ", $tokenizer->tokenize($sentence)), "Sentence: '{$sentence}' was not tokenized correctly");
}
$tokenized->next();
}
}
示例15: getClosureCode
private function getClosureCode(\Closure $closure)
{
$reflection = new \ReflectionFunction($closure);
// Open file and seek to the first line of the closure
$file = new \SplFileObject($reflection->getFileName());
$file->seek($reflection->getStartLine() - 1);
// Retrieve all of the lines that contain code for the closure
$code = '';
while ($file->key() < $reflection->getEndLine()) {
$line = $file->current();
$line = ltrim($line);
$code .= $line;
$file->next();
}
return $code;
}