php将文本文件转换csv输出的方法_.docx

上传人:啊飒飒 文档编号:11626423 上传时间:2021-08-26 格式:DOCX 页数:8 大小:13.48KB
返回 下载 相关 举报
php将文本文件转换csv输出的方法_.docx_第1页
第1页 / 共8页
php将文本文件转换csv输出的方法_.docx_第2页
第2页 / 共8页
php将文本文件转换csv输出的方法_.docx_第3页
第3页 / 共8页
php将文本文件转换csv输出的方法_.docx_第4页
第4页 / 共8页
php将文本文件转换csv输出的方法_.docx_第5页
第5页 / 共8页
亲,该文档总共8页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《php将文本文件转换csv输出的方法_.docx》由会员分享,可在线阅读,更多相关《php将文本文件转换csv输出的方法_.docx(8页珍藏版)》请在三一文库上搜索。

1、php将文本文件转换csv输出的方法_ 这篇文章主要介绍了php将文本文件转换csv输出的方法,通过对SplFileObject类的继承与扩展实现文本文件转换输出的功能,是特别有用的技巧,需要的伴侣可以参考下 本文实例讲解并描述了php将文本文件转换csv输出的方法。分享给大家供大家参考。具体实现方法如下: 这个类供应了转换成固定宽度的CSV文件,快速,简便的方法,它可将SplFileObject用于执行迭代,使它特别高效的一个迭代只知道当前成员,期权是供应给指定行字符和字段分隔符结束,This from CSV files.这个类是格外有用的,假如数据需要来自一个固定宽度的文件,并插入到数据

2、库中,由于大多数的数据库支持从CSV文件中的数据输入. 这一类的便利的功能是可以跳过字段假如不是在输出需要,该领域的阵列供应,供应了一个键/值对,与主要持有的价值偏移,或启动领域的地位,和值包含的宽度,或字段的长度,For example.例如,12 =“10是一个领域,在12位和宽度或字段的长度为10个字符开头. 底的行字符默认成“ n”,而是可以设置为任何字符。 分隔符默认为一个逗号,但可以设置为任何字符,或字符。 从文件的输出可以挺直用法,写入一个文件,到数据库或任何其他目的插入. PHP实例代码如下: 代码如下: ?php /* * Class to convert fixed wid

3、th files into CSV format * Allows to set fields, separator, and end-of-line character * * author Kevin Waterson * url * version $Id$ * */ class fixed2CSV extends SplFileObject /* * * Constructor, duh, calls the parent constructor * * access public * param string The full path to the file to be conve

4、rted * */ public function _construct ( $filename ) parent : _construct ( $filename ); /* * Settor, is called when trying to assign a value to non-existing property * * access public * param string $name The name of the property to set * param mixed $value The value of the property * throw Excption i

5、f property is not able to be set * */ public function _set ( $name , $value ) switch( $name ) case eol : case fields : case separator : $this - $name = $value ; break; default: throw new Exception ( Unable to set $name ); /* * * Gettor This is called when trying to access a non-existing property * *

6、 access public * param string $name The name of the property * throw Exception if proplerty cannot be set * return string * */ public function _get ( $name ) switch( $name ) case eol : return ; case fields : return array(); case separator : return , ; default: throw new Exception ( $name cannot be s

7、et ); /* * * Over ride the parent current method and convert the lines * * access public * return string The line as a CSV representation of the fixed width line, false otherwise * */ public function current () if( parent : current () ) $csv = ; $fields = new cachingIterator ( new ArrayIterator ( $t

8、his - fields ) ); foreach( $fields as $f ) $csv .= trim ( substr ( parent : current (), $fields - key (), $fields - current () ) ); $csv .= $fields - hasNext () ? $this - separator : $this - eol ; return $csv ; return false ; / end of class ? Example Usage示例用法 代码如下: ?php try /* the fixed width file

9、to convert */ $file = new fixed2CSV ( my_file.txt ); /* The start position=width of each field */ $file - fields = array( 0 = 10 , 10 = 15 , 25 = 20 , 45 = 25 ); /* output the converted lines */ foreach( $file as $line ) echo $line ; /* a new instance */ $new = new fixed2CSV ( my_file.txt ); /* get only first and third fields */ $new - fields = array( 0 = 10 , 25 = 20 ); /* output only the first and third fields */ foreach( $new as $line ) echo $line ; catch( Exception $e ) echo $e - getMessage (); ? 盼望本文所述对大家的php程序设计有所关心。 更多信息请查看IT技术专栏 .

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1