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 多维数组排序(usort,uasort)
Jun 30 PHP
使用dump函数,给php加断点测试
Jun 25 PHP
codeigniter使用技巧批量插入数据实例方法分享
Dec 31 PHP
php使用fputcsv()函数csv文件读写数据的方法
Jan 06 PHP
php+xml编程之SimpleXML的应用实例
Jan 24 PHP
PHP中返回引用类型的方法
Apr 03 PHP
PHP基于cookie与session统计网站访问量并输出显示的方法
Jan 15 PHP
php 无限分类 树形数据格式化代码
Oct 11 PHP
微信开发之php表单微信中自动提交两次问题解决办法
Jan 08 PHP
理清PHP在Linxu下执行时的文件权限方法
Jun 07 PHP
PHP实现基于面向对象的mysqli扩展库增删改查操作工具类
Jul 18 PHP
基于thinkphp6.0的success、error实现方法
Nov 05 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调用C#开发的dll类库方法
2014/07/28 PHP
php cli模式下获取参数的方法
2017/05/05 PHP
js判断输入是否为正整数、浮点数等数字的函数代码
2010/11/17 Javascript
js关闭父窗口时关闭子窗口
2013/04/01 Javascript
基于jQuery中对数组进行操作的方法
2013/04/16 Javascript
jquery 获取表单元素里面的值示例代码
2013/07/28 Javascript
Get中文乱码IE浏览器Get中文乱码解决方案
2013/12/26 Javascript
JavaScript按位运算符的应用简析
2014/02/04 Javascript
JavaScript实现继承的4种方法总结
2014/10/16 Javascript
纯javascript实现四方向文本无缝滚动效果
2015/06/16 Javascript
JavaScript获取表格(table)当前行的值、删除行、增加行
2015/07/03 Javascript
JS中解决谷歌浏览器记住密码输入框颜色改变功能
2017/02/13 Javascript
JS简单获取日期相差天数的方法
2017/04/24 Javascript
React Native悬浮按钮组件的示例代码
2018/04/05 Javascript
create-react-app 修改为多入口编译的方法
2018/08/01 Javascript
ES6 let和const定义变量与常量的应用实例分析
2019/06/27 Javascript
JS绘图Flot如何实现可选显示曲线图功能
2020/10/16 Javascript
python自动化测试实例解析
2014/09/28 Python
跟老齐学Python之有容乃大的list(4)
2014/09/28 Python
使用Python实现下载网易云音乐的高清MV
2015/03/16 Python
开源软件包和环境管理系统Anaconda的安装使用
2017/09/04 Python
python登录并爬取淘宝信息代码示例
2017/12/09 Python
对python 合并 累加两个dict的实例详解
2019/01/21 Python
Python爬虫实现爬取百度百科词条功能实例
2019/04/05 Python
PyQt5实现QLineEdit添加clicked信号的方法
2019/06/25 Python
Python Pandas中根据列的值选取多行数据
2019/07/08 Python
Spartoo西班牙官网:法国时尚购物网站
2018/03/27 全球购物
Ralph Lauren英国官方网站:Ralph Lauren UK
2018/04/03 全球购物
波兰家居和花园家具专家:4Home
2019/05/26 全球购物
行政助理的职责
2013/11/14 职场文书
洗手间标语
2014/06/23 职场文书
责任书范本
2014/08/25 职场文书
2015年教师节活动总结
2015/03/20 职场文书
优秀团员主要事迹范文
2015/11/05 职场文书
使用golang编写一个并发工作队列
2021/05/08 Golang