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
PHP新手上路(三)
Oct 09 PHP
php 文章调用类代码
Aug 11 PHP
30 个很棒的PHP开源CMS内容管理系统小结
Oct 14 PHP
header中Content-Disposition的作用与使用方法
Jun 13 PHP
PHP类继承 extends使用介绍
Jan 14 PHP
如何阻止网站被恶意反向代理访问(防网站镜像)
Mar 18 PHP
php cookie中点号(句号)自动转为下划线问题
Oct 21 PHP
phpStudy2016 配置多个域名期间遇到的问题小结
Oct 19 PHP
Bootstrap+PHP实现多图上传功能实例详解
Apr 08 PHP
Laravel5.5以下版本中如何自定义日志行为详解
Aug 01 PHP
php去除deprecated的实例方法
Nov 17 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
将二维数组转为一维数组的2种方法
2014/05/26 PHP
php中strstr、strrchr、substr、stristr四个函数的区别总结
2014/09/22 PHP
thinkphp多层MVC用法分析
2015/12/30 PHP
浅谈mysql_query()函数的返回值问题
2016/09/05 PHP
php使用json-schema模块实现json校验示例
2019/09/28 PHP
常用jQuery选择器总结
2014/07/11 Javascript
javascript常用函数归纳整理
2014/10/31 Javascript
JavaScript中的比较操作符&gt;、=、
2014/12/31 Javascript
javascript数据类型示例分享
2015/01/19 Javascript
JS操作HTML自定义属性的方法
2015/02/10 Javascript
Prototype框架详解
2015/11/25 Javascript
原生js和jQuery实现淡入淡出轮播效果
2015/12/25 Javascript
javascript对浅拷贝和深拷贝的详解
2016/10/14 Javascript
headjs实现网站并行加载但顺序执行JS
2016/11/29 Javascript
NodeJs的fs读写删除移动监听
2017/04/28 NodeJs
集成vue到jquery/bootstrap项目的方法
2018/02/10 jQuery
axios对请求各种异常情况处理的封装方法
2018/09/25 Javascript
vue-cli3搭建项目的详细步骤
2018/12/05 Javascript
Vue实现点击显示不同图片的效果
2019/08/10 Javascript
JS事件流与事件处理程序实例分析
2019/08/16 Javascript
JS中自定义事件的使用与触发操作实例分析
2019/11/01 Javascript
Vue项目中使用jsonp抓取跨域数据的方法
2019/11/10 Javascript
[01:03:54]Liquid vs IG 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/18 DOTA
python实现将html表格转换成CSV文件的方法
2015/06/28 Python
Python实现图片添加文字
2019/11/26 Python
html5 跨文档消息传输示例探讨
2013/04/01 HTML / CSS
AmazeUI在模态框中嵌入表单形成模态输入框
2020/08/20 HTML / CSS
Pedro官网:新加坡时尚品牌
2019/08/27 全球购物
德国婴儿服装和婴儿用品购买网站:Baby Sweets
2019/12/08 全球购物
人力资源专业推荐信
2013/11/29 职场文书
幼儿园师德演讲稿
2014/05/06 职场文书
求职信的正确写法
2014/07/10 职场文书
学校开学标语
2014/10/06 职场文书
2014年检验科工作总结
2014/11/22 职场文书
单身申明具结书
2015/02/26 职场文书
家长对孩子的寄语
2015/02/26 职场文书