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的分页功能
Mar 21 PHP
php 上传文件类型判断函数(避免上传漏洞 )
Jun 08 PHP
php更改目录及子目录下所有的文件后缀扩展名的代码
Oct 12 PHP
php设计模式 Decorator(装饰模式)
Jun 26 PHP
超级实用的7个PHP代码片段分享
Jan 05 PHP
php中数组首字符过滤功能代码
Jul 31 PHP
实例讲解PHP面向对象之多态
Aug 20 PHP
完美利用Yii2微信后台开发的系列总结
Jul 18 PHP
PHP内置函数生成随机数实例
Jan 18 PHP
php使用mysqli和pdo扩展,测试对比连接mysql数据库的效率完整示例
May 09 PHP
php 使用expat方式解析xml文件操作示例
Nov 26 PHP
php设计模式之建造器模式分析【星际争霸游戏案例】
Jan 23 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开发框架总结收藏
2008/04/24 PHP
PHP中替换换行符的几种方法小结
2012/10/15 PHP
关于PHP实现异步操作的研究
2013/02/03 PHP
Codeigniter操作数据库表的优化写法总结
2014/06/12 PHP
php打印一个边长为N的实心和空心菱型的方法
2015/03/02 PHP
详解PHP使用Redis存储session时的一个Warning定位
2017/07/05 PHP
jquery创建div 实现代码
2009/04/27 Javascript
JQuery 选择和过滤方法代码总结
2010/11/19 Javascript
js 触发select onchange事件代码
2014/03/20 Javascript
JS实现向表格行添加新单元格的方法
2015/03/30 Javascript
JQuery判断radio(单选框)是否选中和获取选中值方法总结
2015/04/15 Javascript
JavaScript中数据结构与算法(四):串(BF)
2015/06/19 Javascript
js图片切换具体实现代码
2016/10/13 Javascript
javascript垃圾收集机制的原理分析
2016/12/08 Javascript
vue-cli初始化项目中使用less的方法
2018/08/09 Javascript
jQuery实现鼠标移到某个对象时弹出显示层功能
2018/08/23 jQuery
vue 的 solt 子组件过滤过程解析
2019/09/07 Javascript
[05:13]TI4 中国战队 机场出征!!
2014/07/07 DOTA
使用python装饰器验证配置文件示例
2014/02/24 Python
python读取浮点数和读取文本文件示例
2014/05/06 Python
详解Python中的静态方法与类成员方法
2017/02/28 Python
Python爬虫之正则表达式基本用法实例分析
2018/08/08 Python
python之pyqt5通过按钮改变Label的背景颜色方法
2019/06/13 Python
Python 中PyQt5 点击主窗口弹出另一个窗口的实现方法
2019/07/04 Python
Django之提交表单与前后端交互的方法
2019/07/19 Python
PyCharm无法识别PyQt5的2种解决方法,ModuleNotFoundError: No module named 'pyqt5'
2020/02/17 Python
python中requests模拟登录的三种方式(携带cookie/session进行请求网站)
2020/11/17 Python
基于python实现监听Rabbitmq系统日志代码示例
2020/11/28 Python
Myprotein瑞士官方网站:运动营养和健身网上商店
2019/09/25 全球购物
ktv总经理岗位职责
2014/02/17 职场文书
监察建议书格式
2014/05/19 职场文书
道路交通事故人身损害赔偿协议书
2014/11/19 职场文书
2015年党务公开工作总结
2015/05/19 职场文书
庆祝教师节主题班会
2015/08/17 职场文书
提档介绍信范文
2015/10/22 职场文书
《模拟人生4》推出新补丁 “婚礼奇缘”DLC终于得到修复
2022/04/03 其他游戏