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 相关文章推荐
实现“上一页”和“下一页按钮
Oct 09 PHP
[原创]效率较高的php下读取文本文件的代码
Jul 02 PHP
php对二维数组按指定键值key排序示例代码
Nov 26 PHP
PHP之APC缓存详细介绍 apc模块安装
Jan 13 PHP
PHP使用PDO连接ACCESS数据库
Mar 05 PHP
PHP实现操作redis的封装类完整实例
Nov 14 PHP
php curl中gzip的压缩性能测试实例分析
Nov 08 PHP
PHP magento后台无法登录问题解决方法
Nov 24 PHP
利用PHP判断文件是否为图片的方法总结
Jan 06 PHP
PHP基于GD2函数库实现验证码功能示例
Jan 27 PHP
PHP attributes()函数讲解
Feb 03 PHP
PHP程序员简单的开展服务治理架构操作详解(一)
May 14 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中养成7个面向对象的好习惯
2010/07/17 PHP
Avengerls vs KG BO3 第二场2.18
2021/03/10 DOTA
用js实现多域名不同文件的调用方法
2007/01/12 Javascript
总结AJAX相关JS代码片段和浏览器模型
2007/08/15 Javascript
jQuery height()、innerHeight()、outerHeight()函数的区别详解
2016/05/23 Javascript
JS组件Bootstrap Table使用实例分享
2016/05/30 Javascript
vue的props实现子组件随父组件一起变化
2016/10/27 Javascript
jQuery post数据至ashx实例详解
2016/11/18 Javascript
JavaScript字符串检索字符的方法
2017/06/23 Javascript
详谈表单重复提交的三种情况及解决方法
2017/08/16 Javascript
微信小程序之数据缓存的实例详解
2017/09/29 Javascript
JS实现可视化文件上传
2018/09/08 Javascript
js JSON.stringify()基础详解
2019/06/19 Javascript
vue 实现走马灯效果
2019/10/28 Javascript
JavaScript面向对象核心知识与概念归纳整理
2020/05/09 Javascript
Jquery cookie插件实现原理代码解析
2020/08/04 jQuery
在Vue中使用HOC模式的实现
2020/08/23 Javascript
Python创建文件和追加文件内容实例
2014/10/21 Python
python生成验证码图片代码分享
2016/01/28 Python
简单谈谈python的反射机制
2016/06/28 Python
Python 判断文件或目录是否存在的实例代码
2018/07/19 Python
解决Python 使用h5py加载文件,看不到keys()的问题
2019/02/08 Python
Python学习笔记之变量、自定义函数用法示例
2019/05/28 Python
python for循环remove同一个list过程解析
2019/08/14 Python
在pycharm中显示python画的图方法
2019/08/31 Python
Python统计文本词汇出现次数的实例代码
2020/02/27 Python
如何以Winsows Service方式运行JupyterLab
2020/08/30 Python
利用Python将图片中扭曲矩形的复原
2020/09/07 Python
Django Model层F,Q对象和聚合函数原理解析
2020/11/12 Python
瑞典领先的汽车零部件网上零售商:bildelaronline24.se
2017/01/12 全球购物
英国泽西岛植物:Jersey Plants Direct
2019/08/07 全球购物
入党心得体会
2019/06/20 职场文书
承诺书应该怎么写?
2019/09/10 职场文书
导游词之江南园林狮子林
2019/09/16 职场文书
nginx处理http请求实现过程解析
2021/03/31 Servers
js实现模拟购物商城案例
2021/05/18 Javascript