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中用正则表达式清除字符串的空白
Jan 17 PHP
mysql,mysqli,PDO的各自不同介绍
Sep 19 PHP
php 中文字符串首字母的获取函数分享
Nov 04 PHP
php中sql注入漏洞示例 sql注入漏洞修复
Jan 24 PHP
PHP解密Unicode及Escape加密字符串
May 17 PHP
老生常谈PHP面向对象之命令模式(必看篇)
May 24 PHP
利用PHPStorm如何开发Laravel应用详解
Aug 30 PHP
Ubuntu上安装yaf扩展的方法
Jan 29 PHP
php处理多图上传压缩代码功能
Jun 13 PHP
PHP依赖注入原理与用法分析
Aug 21 PHP
PHP中使用CURL发送get/post请求上传图片批处理功能
Oct 15 PHP
关于Yii2框架跑脚本时内存泄漏问题的分析与解决
Dec 01 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
图书管理程序(二)
2006/10/09 PHP
关于Appserv无法打开localhost问题的解决方法
2009/10/16 PHP
redis 队列操作的例子(php)
2012/04/12 PHP
php实现邮件发送并带有附件
2014/01/24 PHP
php中print(),print_r(),echo()的区别详解
2014/12/01 PHP
javascript编程起步(第五课)
2007/02/27 Javascript
初窥JQuery(二) 事件机制(1)
2010/11/25 Javascript
幻灯片带网页设计中的20个奇妙应用示例小结
2012/05/27 Javascript
利用jq让你的div居中的好方法分享
2013/11/21 Javascript
AngularJs根据访问的页面动态加载Controller的解决方案
2015/02/04 Javascript
基于jquery实现的树形菜单效果代码
2015/09/06 Javascript
基于jQuery Tipso插件实现消息提示框特效
2016/03/16 Javascript
Bootstrap Table使用方法详解
2016/08/01 Javascript
详解vue之页面缓存问题(基于2.0)
2017/01/10 Javascript
React Native中导航组件react-navigation跨tab路由处理详解
2017/10/31 Javascript
nodejs实现超简单生成二维码的方法
2018/03/17 NodeJs
快速实现基于Python的微信聊天机器人示例代码
2017/03/03 Python
一个月入门Python爬虫学习,轻松爬取大规模数据
2018/01/03 Python
理想高通滤波实现Python opencv示例
2019/01/30 Python
Django框架会话技术实例分析【Cookie与Session】
2019/05/24 Python
Python批量查询关键词微信指数实例方法
2019/06/27 Python
Python字典中的值为列表或字典的构造实例
2019/12/16 Python
Python多线程多进程实例对比解析
2020/03/12 Python
Tensorflow tf.nn.atrous_conv2d如何实现空洞卷积的
2020/04/20 Python
Django+Celery实现动态配置定时任务的方法示例
2020/05/26 Python
详解python使用金山词霸的翻译功能(调试工具断点的使用)
2021/01/07 Python
python+selenium爬取微博热搜存入Mysql的实现方法
2021/01/27 Python
html5音频_动力节点Java学院整理
2018/08/22 HTML / CSS
WEB控件及HTML服务端控件能否调用客户端方法?如果能,请解释如何调用?
2015/08/25 面试题
四川internet信息高速公路(C#)笔试题
2012/02/29 面试题
法学毕业生自我鉴定
2013/11/08 职场文书
敬老月活动总结
2014/08/28 职场文书
具结保证书
2015/01/17 职场文书
村官个人总结范文
2015/03/03 职场文书
公司市场部岗位职责
2015/04/15 职场文书
带你了解Java中的ForkJoin
2022/04/28 Java/Android