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


PHP csv_import_reader::get_encoded_delimiter方法代码示例

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


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

示例1: load_csv_content

 /**
  * Parse this content
  *
  * @global object
  * @global object
  * @param string $content passed by ref for memory reasons, unset after return
  * @param string $encoding content encoding
  * @param string $delimiter_name separator (comma, semicolon, colon, cfg)
  * @param string $column_validation name of function for columns validation, must have one param $columns
  * @return bool false if error, count of data lines if ok; use get_error() to get error string
  */
 function load_csv_content(&$content, $encoding, $delimiter_name, $column_validation = null)
 {
     global $USER, $CFG;
     $this->close();
     $this->_error = null;
     $content = textlib::convert($content, $encoding, 'utf-8');
     // remove Unicode BOM from first line
     $content = textlib::trim_utf8_bom($content);
     // Fix mac/dos newlines
     $content = preg_replace('!\\r\\n?!', "\n", $content);
     // is there anyting in file?
     $columns = strtok($content, "\n");
     if ($columns === false) {
         $this->_error = get_string('csvemptyfile', 'error');
         return false;
     }
     $csv_delimiter = csv_import_reader::get_delimiter($delimiter_name);
     $csv_encode = csv_import_reader::get_encoded_delimiter($delimiter_name);
     // process header - list of columns
     $columns = explode($csv_delimiter, $columns);
     $col_count = count($columns);
     if ($col_count === 0) {
         $this->_error = get_string('csvemptyfile', 'error');
         return false;
     }
     foreach ($columns as $key => $value) {
         $columns[$key] = str_replace($csv_encode, $csv_delimiter, trim($value));
     }
     if ($column_validation) {
         $result = $column_validation($columns);
         if ($result !== true) {
             $this->_error = $result;
             return false;
         }
     }
     $this->_columns = $columns;
     // cached columns
     // open file for writing
     $filename = $CFG->tempdir . '/csvimport/' . $this->_type . '/' . $USER->id . '/' . $this->_iid;
     $fp = fopen($filename, "w");
     fwrite($fp, serialize($columns) . "\n");
     // again - do we have any data for processing?
     $line = strtok("\n");
     $data_count = 0;
     while ($line !== false) {
         $line = explode($csv_delimiter, $line);
         foreach ($line as $key => $value) {
             $line[$key] = str_replace($csv_encode, $csv_delimiter, trim($value));
         }
         if (count($line) !== $col_count) {
             // this is critical!!
             $this->_error = get_string('csvweirdcolumns', 'error');
             fclose($fp);
             $this->cleanup();
             return false;
         }
         fwrite($fp, serialize($line) . "\n");
         $data_count++;
         $line = strtok("\n");
     }
     fclose($fp);
     return $data_count;
 }
开发者ID:saurabh947,项目名称:MoodleLearning,代码行数:74,代码来源:csvlib.class.php


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