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
Zend Studio for Eclipse的java.lang.NullPointerException错误的解决方法
Dec 06 PHP
PHP 调试工具Debug Tools
Apr 30 PHP
在PHP中运行Linux命令并启动SSH服务的例子
Jun 12 PHP
php校验表单检测字段是否为空的方法
Mar 20 PHP
PHP实现的蚂蚁爬杆路径算法代码
Dec 03 PHP
PHP的Yii框架中移除组件所绑定的行为的方法
Mar 18 PHP
详解php伪造Referer请求反盗链资源
Jan 24 PHP
PDO::exec讲解
Jan 28 PHP
YII框架页面缓存操作示例
Apr 29 PHP
PHP CURL中传递cookie的方法步骤
May 09 PHP
PHP 8新特性简介
Aug 18 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
phpmyadmin 常用选项设置详解版
2010/03/07 PHP
php中使用redis队列操作实例代码
2013/02/07 PHP
使用php将某个目录下面的所有文件罗列出来的方法详解
2013/06/21 PHP
一个非常完美的读写ini格式的PHP配置类分享
2015/02/12 PHP
ThinkPHP使用getlist方法实现数据搜索功能示例
2017/05/08 PHP
innertext , insertadjacentelement , insertadjacenthtml , insertadjacenttext 等区别
2007/06/29 Javascript
javascript 面向对象编程  function是方法(函数)
2009/09/17 Javascript
理解Javascript_01_理解内存分配原理分析
2010/10/11 Javascript
JavaScript通过RegExp实现客户端验证处理程序
2013/05/07 Javascript
node.js中使用socket.io制作命名空间
2014/12/15 Javascript
JQuery radio(单选按钮)操作方法汇总
2015/04/15 Javascript
js限制文本框只能输入整数或者带小数点的数字
2015/04/27 Javascript
jQuery实现表格展开与折叠的方法
2015/05/04 Javascript
jQuery实现动画效果circle实例
2015/08/06 Javascript
使用jquery插件qrcode生成二维码
2015/10/22 Javascript
把多个JavaScript函数绑定到onload事件处理函数上的方法
2016/09/04 Javascript
详谈Ajax请求中的async:false/true的作用(ajax 在外部调用问题)
2017/02/10 Javascript
Node接收电子邮件的实例代码
2017/07/21 Javascript
vue history 模式打包部署在域名的二级目录的配置指南
2019/07/02 Javascript
JS根据Unix时间戳显示发布时间是多久前【项目实测】
2019/07/10 Javascript
layui操作列按钮个数和文字颜色的判断实例
2019/09/11 Javascript
ES6 async、await的基本使用方法示例
2020/06/06 Javascript
[01:16:16]DOTA2-DPC中国联赛定级赛 RNG vs Phoenix BO3第二场 1月8日
2021/03/11 DOTA
Python中实现参数类型检查的简单方法
2015/04/21 Python
Python网络爬虫项目:内容提取器的定义
2016/10/25 Python
实现CSS3中的border-radius(边框圆角)示例代码
2013/07/19 HTML / CSS
金讯Java笔试题目
2013/06/18 面试题
教师年终个人自我评价
2013/10/04 职场文书
行政办公员自我评价分享
2013/12/14 职场文书
学习心得体会
2014/01/01 职场文书
党的群众路线教育实践活动公开承诺书
2014/03/28 职场文书
幼儿园家长评语大全
2014/04/16 职场文书
《惊弓之鸟》教学反思
2016/02/20 职场文书
python文件名批量重命名脚本实例代码
2021/04/22 Python
python munch库的使用解析
2021/05/25 Python
Android Studio实现简易进制转换计算器
2022/05/20 Java/Android