當前位置: 首頁>>代碼示例>>PHP>>正文


PHP SplFileObject::next方法代碼示例

本文整理匯總了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;
 }
開發者ID:stedop,項目名稱:playground-framework,代碼行數:12,代碼來源:BasicFileHandler.php

示例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);
}
開發者ID:panchao1,項目名稱:my_php_code,代碼行數:42,代碼來源:readFile.php

示例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;
     }
 }
開發者ID:pixcero,項目名稱:bugs,代碼行數:36,代碼來源:Report.php

示例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;
 }
開發者ID:schigh,項目名稱:router,代碼行數:33,代碼來源:crouter.php

示例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;
 }
開發者ID:rushi,項目名稱:XolaReportWriterBundle,代碼行數:65,代碼來源:CSVWriter.php

示例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;
 }
開發者ID:NightWingStudios,項目名稱:Nightshade,代碼行數:14,代碼來源:Language.php

示例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";
 }
開發者ID:branJakJak,項目名稱:dienSiSystem,代碼行數:56,代碼來源:ImportBlackListMobileCommand.php

示例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();
     }
 }
開發者ID:mothership-ec,項目名稱:cog,代碼行數:10,代碼來源:CSVFile.php

示例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;
}
開發者ID:Antoine07,項目名稱:AppFromScratch,代碼行數:11,代碼來源:seeds.php

示例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);
 }
開發者ID:jceee,項目名稱:ikaros,代碼行數:19,代碼來源:FileFunc.php

示例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;
 }
開發者ID:Fadheler,項目名稱:TorrentTrader-3.0,代碼行數:13,代碼來源:Upload.php

示例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()));
     }
 }
開發者ID:jne21,項目名稱:hr_test_jne21,代碼行數:13,代碼來源:Task3Test.php

示例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();
         }
     }
 }
開發者ID:GemsTracker,項目名稱:MUtil,代碼行數:16,代碼來源:FileIteratorAbstract.php

示例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();
     }
 }
開發者ID:WHATNEXTLIMITED,項目名稱:php-text-analysis,代碼行數:15,代碼來源:PennTreeBankTokenizerTest.php

示例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;
 }
開發者ID:ryosukemiyazawa,項目名稱:lasa,代碼行數:16,代碼來源:ArrayLoader.php


注:本文中的SplFileObject::next方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。