当前位置: 首页>>代码示例>>PHP>>正文


PHP Iterator::currentPosition方法代码示例

本文整理汇总了PHP中Iterator::currentPosition方法的典型用法代码示例。如果您正苦于以下问题:PHP Iterator::currentPosition方法的具体用法?PHP Iterator::currentPosition怎么用?PHP Iterator::currentPosition使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Iterator的用法示例。


在下文中一共展示了Iterator::currentPosition方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: parse


//.........这里部分代码省略.........
         // The actual parser. explode() is unfortunately not suitable because the
         // delimiter might be located inside a quoted field, and that would break
         // the field and/or require additional effort to re-join the fields.
         $quoted = FALSE;
         $current_index = 0;
         $current_field = '';
         $fields = array();
         // We must use strlen() as we're parsing byte by byte using strpos(), so
         // drupal_strlen() will not work properly.
         $line_length = strlen($line);
         while ($current_index <= $line_length) {
             if ($quoted) {
                 $next_quote_index = strpos($line, '"', $current_index);
                 if ($next_quote_index === FALSE) {
                     // There's a line break before the quote is closed, so grab the rest
                     // of this line and fetch the next line.
                     $current_field .= substr($line, $current_index);
                     $line_iterator->next();
                     if (!$line_iterator->valid()) {
                         // Whoa, an unclosed quote! Well whatever, let's just ignore
                         // that shortcoming and record it nevertheless.
                         $fields[] = $current_field;
                         break;
                     }
                     // Ok, so, on with fetching the next line, as mentioned above.
                     $current_field .= "\n";
                     $line = trim($line_iterator->current(), "\r\n");
                     $current_index = 0;
                     continue;
                 }
                 // There's actually another quote in this line, find out whether it's
                 // escaped or not.
                 $current_field .= substr($line, $current_index, $next_quote_index - $current_index);
                 if (isset($line[$next_quote_index + 1]) && $line[$next_quote_index + 1] === '"') {
                     // Escaped quote, add a single one to the field and proceed quoted.
                     $current_field .= '"';
                     $current_index = $next_quote_index + 2;
                 } else {
                     // End of the quoted section, close the quote and let the
                     // $quoted == FALSE block finalize the field.
                     $quoted = FALSE;
                     $current_index = $next_quote_index + 1;
                 }
             } else {
                 // First, let's find out where the next character of interest is.
                 $next_quote_index = strpos($line, '"', $current_index);
                 $next_delimiter_index = strpos($line, $this->delimiter, $current_index);
                 if ($next_quote_index === FALSE) {
                     $next_index = $next_delimiter_index;
                 } elseif ($next_delimiter_index === FALSE) {
                     $next_index = $next_quote_index;
                 } else {
                     $next_index = min($next_quote_index, $next_delimiter_index);
                 }
                 if ($next_index === FALSE) {
                     // This line is done, add the rest of it as last field.
                     $current_field .= substr($line, $current_index);
                     $fields[] = $current_field;
                     break;
                 } elseif ($line[$next_index] === $this->delimiter[0]) {
                     $length = $next_index + strlen($this->delimiter) - 1 - $current_index;
                     $current_field .= substr($line, $current_index, $length);
                     $fields[] = $current_field;
                     $current_field = '';
                     $current_index += $length + 1;
                     // Continue with the next field.
                 } else {
                     $quoted = TRUE;
                     $current_field .= substr($line, $current_index, $next_index - $current_index);
                     $current_index = $next_index + 1;
                     // Continue this field in the $quoted == TRUE block.
                 }
             }
         }
         // End of CSV parser. We've now got all the fields of the line as strings
         // in the $fields array.
         if (!$this->columnNames) {
             $row = $fields;
         } else {
             $row = array();
             foreach ($this->columnNames as $column_name) {
                 $field = array_shift($fields);
                 $row[$column_name] = (string) $field;
             }
         }
         $rows[] = $row;
         // Quit parsing if timeout has been reached or requested lines have been
         // reached.
         if ($max_time && microtime() > $max_time) {
             $this->lastLinePos = $line_iterator->currentPosition();
             break;
         }
         $lines_parsed++;
         if ($this->lineLimit && $lines_parsed >= $this->lineLimit) {
             $this->lastLinePos = $line_iterator->currentPosition();
             break;
         }
     }
     return $rows;
 }
开发者ID:alnutile,项目名称:drunatra,代码行数:101,代码来源:ParserCSV.php


注:本文中的Iterator::currentPosition方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。