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


Posted in PHP onDecember 31, 2014

本文实例讲述了php将文本文件转换csv输出的方法。分享给大家供大家参考。具体实现方法如下:

这个类提供了转换成固定宽度的CSV文件,快速,简便的方法,它可将SplFileObject用于执行迭代,使它非常高效的一个迭代只知道当前成员,期权是提供给指定行字符和字段分隔符结束,This from CSV files.这个类是特别有用的,如果数据需要来自一个固定宽度的文件,并插入到数据库中,因为大多数的数据库支持从CSV文件中的数据输入.

这一类的方便的功能是可以跳过字段如果不是在输出需要,该领域的阵列提供,提供了一个键/值对,与主要持有的价值偏移,或启动领域的地位,和值包含的宽度,或字段的长度,For example.例如,12 =“10是一个领域,在12位和宽度或字段的长度为10个字符开始.

底的行字符默认成“ n”,而是可以设置为任何字符。

分隔符默认为一个逗号,但可以设置为任何字符,或字符。

从文件的输出可以直接使用,写入一个文件,到数据库或任何其他目的插入.

PHP实例代码如下:

<?php 

/**  

* Class to convert fixed width files into CSV format  

* Allows to set fields, separator, and end-of-line character  

*  

* @author Kevin Waterson  

* @url http://phpro.org  

* @version $Id$  

*  

*/  

class fixed2CSV extends SplFileObject  

{  

/**  

*  

* Constructor, duh, calls the parent constructor  

*  

* @access       public  

* @param    string  The full path to the file to be converted  

*  

*/  

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 if 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  

*  

* @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 set" );  

}  

} 

 

/**  

*  

* 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 ( $this -> 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 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程序设计有所帮助。

PHP 相关文章推荐
PHP 简单日历实现代码
Oct 28 PHP
延长phpmyadmin登录时间的方法
Feb 06 PHP
一个PHP分页类的代码
May 18 PHP
解析用PHP读写音频文件信息的详解(支持WMA和MP3)
May 10 PHP
smarty半小时快速上手入门教程
Oct 27 PHP
Yii2中OAuth扩展及QQ互联登录实现方法
May 16 PHP
php中请求url的五种方法总结
Jul 13 PHP
PHP+mysql实现的三级联动菜单功能示例
Feb 15 PHP
tp5递归 无限级分类详解
Oct 18 PHP
在Laravel中使用MongoDB的方法示例
Nov 11 PHP
PHP获取真实IP及IP模拟方法解析
Nov 24 PHP
通过代码实例解析PHP session工作原理
Dec 11 PHP
19个Android常用工具类汇总
Dec 30 #PHP
php+ajax实现文章自动保存的方法
Dec 30 #PHP
php实现监控varnish缓存服务器的状态
Dec 30 #PHP
php在线解压ZIP文件的方法
Dec 30 #PHP
php站内搜索关键词变亮的实现方法
Dec 30 #PHP
php使用PDO操作MySQL数据库实例
Dec 30 #PHP
discuz目录文件资料汇总
Dec 30 #PHP
You might like
用PHP来写记数器(详细介绍)
2006/10/09 PHP
PHP 第二节 数据类型之转换
2012/04/28 PHP
一致性哈希算法以及其PHP实现详细解析
2013/08/24 PHP
Flash对联广告的关闭按钮讨论
2007/01/30 Javascript
JS的递增/递减运算符和带操作的赋值运算符的等价式
2007/12/08 Javascript
JS前端框架关于重构的失败经验分享
2013/03/17 Javascript
javaScript NameSpace 简单说明介绍
2013/07/18 Javascript
JavaScript判断密码强度(自写代码)
2013/09/06 Javascript
jquery的父子兄弟节点查找示例代码
2014/03/03 Javascript
jQuery中 delegate使用的问题
2015/07/03 Javascript
jquery使用ul模拟select实现表单美化的方法
2015/08/18 Javascript
jQuery添加options点击事件并传值实例代码
2016/05/18 Javascript
react-router实现按需加载
2017/05/09 Javascript
深入学习nodejs中的async模块的使用方法
2017/07/12 NodeJs
jQuery+PHP实现上传裁剪图片
2020/06/29 jQuery
Python中的包和模块实例
2014/11/22 Python
Python解析xml中dom元素的方法
2015/03/12 Python
使用Python脚本来获取Cisco设备信息的示例
2015/05/04 Python
python使用tensorflow深度学习识别验证码
2018/04/03 Python
Python发展简史 Python来历
2019/05/14 Python
通过PHP与Python代码对比的语法差异详解
2019/07/10 Python
Python搭建代理IP池实现接口设置与整体调度
2019/10/27 Python
Spring实战之使用util:命名空间简化配置操作示例
2019/12/09 Python
python去除删除数据中\u0000\u0001等unicode字符串的代码
2020/03/06 Python
深入浅析Python 命令行模块 Click
2020/03/11 Python
python线程池如何使用
2020/05/28 Python
python实现企业微信定时发送文本消息的示例代码
2020/11/24 Python
英国羊绒服装购物网站:Pure Collection
2018/10/22 全球购物
数百万免费的图形资源:Freepik
2020/09/21 全球购物
方正Java笔试题
2014/07/03 面试题
最新的互联网创业计划书
2014/01/10 职场文书
考核工作实施方案
2014/03/30 职场文书
分家协议书
2014/04/21 职场文书
股票投资建议书
2014/05/19 职场文书
设备管理实施方案
2014/05/31 职场文书
公司财务制度:成本管理控制制度模板
2019/11/19 职场文书