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 09 PHP
sql注入与转义的php函数代码
Jun 17 PHP
php中如何防止表单的重复提交
Aug 02 PHP
PHP curl 抓取AJAX异步内容示例
Sep 09 PHP
php更新mysql后获取改变行数的方法
Dec 25 PHP
PHP基于单例模式编写PDO类的方法
Sep 13 PHP
Laravel学习教程之本地化模块
Aug 18 PHP
php获取微信共享收货地址的方法
Dec 21 PHP
thinkphp5使用无限极分类
Feb 18 PHP
laravel使用Faker数据填充的实现方法
Apr 12 PHP
PHP的介绍以及优势详细分析
Sep 05 PHP
PHP $O00OO0=urldecode &amp; eval 解密,记一次商业源码的去后门
Sep 13 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 获取目录下的图片并随机显示的代码
2009/12/28 PHP
Yii实现的多级联动下拉菜单
2016/07/13 PHP
thinkPHP5框架实现基于ajax的分页功能示例
2018/06/12 PHP
PHP后台实现微信小程序登录
2018/08/03 PHP
golang实现php里的serialize()和unserialize()序列和反序列方法详解
2018/10/30 PHP
Jquery判断IE6等浏览器的代码
2011/04/05 Javascript
优化Jquery,提升网页加载速度
2013/11/14 Javascript
JavaScript的Number对象的toString()方法
2015/12/18 Javascript
jQuery拖动元素并对元素进行重新排序
2015/12/30 Javascript
JavaScript实现相册弹窗功能(zepto.js)
2016/06/21 Javascript
基于jquery实现的鼠标悬停提示案例
2016/12/11 Javascript
关于foreach循环中遇到的问题小结
2017/05/08 Javascript
react-native fetch的具体使用方法
2017/11/01 Javascript
解决vue做详情页跳转的时候使用created方法 数据不会更新问题
2020/07/24 Javascript
[02:40]DOTA2英雄基础教程 先知
2013/11/29 DOTA
[52:39]完美世界DOTA2联赛PWL S3 CPG vs Forest 第一场 12.16
2020/12/17 DOTA
Python正则表达式如何进行字符串替换实例
2016/12/28 Python
python计算auc指标实例
2017/07/13 Python
代码分析Python地图坐标转换
2018/02/08 Python
Python中__slots__属性介绍与基本使用方法
2018/09/05 Python
python实现剪切功能
2019/01/23 Python
使用tensorflow显示pb模型的所有网络结点方式
2020/01/23 Python
python3正则模块re的使用方法详解
2020/02/11 Python
推荐值得学习的12款python-web开发框架
2020/08/10 Python
浅析Python中字符串的intern机制
2020/10/03 Python
简述python&amp;pytorch 随机种子的实现
2020/10/07 Python
香港No.1得奖零食网:香港零食大王
2016/07/22 全球购物
Puma印度官网:德国运动品牌
2019/10/06 全球购物
英国独特家具和家庭用品购物网站:Cuckooland
2020/08/30 全球购物
父亲八十大寿答谢词
2014/01/23 职场文书
葬礼司仪主持词
2014/03/31 职场文书
2014年入党积极分子党校培训心得体会
2014/07/08 职场文书
医院我们的节日活动实施方案
2014/08/22 职场文书
骨干教师个人总结
2015/02/11 职场文书
2015建军节87周年演讲稿
2015/03/19 职场文书
python实现黄金分割法的示例代码
2021/04/28 Python