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数组函数序列之array_splice() - 在数组任意位置插入元素
Nov 07 PHP
通过PHP current函数获取未知字符键名数组第一个元素的值
Jun 24 PHP
PHP自动识别字符集并完成转码详解
Aug 02 PHP
php递归使用示例(php递归函数)
Feb 14 PHP
jquery+php+ajax显示上传进度的多图片上传并生成缩略图代码
Oct 15 PHP
微信自定义菜单的处理开发示例
Apr 16 PHP
php实现比较两个字符串日期大小的方法
May 12 PHP
php ajax实现文件上传进度条
Mar 29 PHP
CodeIgniter常用知识点小结
May 26 PHP
使用PHP下载CSS文件中的所有图片【几行代码即可实现】
Dec 14 PHP
PHP设计模式之装饰器模式定义与用法详解
Apr 02 PHP
6个常见的 PHP 安全性攻击实例和阻止方法
Dec 16 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代码
2010/08/08 PHP
php检测useragent版本示例
2014/03/24 PHP
UTF-8正则表达式如何匹配汉字
2015/08/03 PHP
CodeIgniter多语言实现方法详解
2016/01/20 PHP
CI框架网页缓存简单用法分析
2018/12/26 PHP
在JavaScript中实现命名空间
2006/11/23 Javascript
另类调用flash无须激活的方法
2006/12/27 Javascript
获取网站跟路径的javascript代码(站点及虚拟目录)
2009/10/20 Javascript
JS声明变量背后的编译原理剖析
2012/12/28 Javascript
JS对话框_JS模态对话框showModalDialog用法总结
2014/01/11 Javascript
jquery 新建的元素事件绑定问题解决方案
2014/06/12 Javascript
微信JSSDK上传图片
2015/08/23 Javascript
论Bootstrap3和Foundation5网格系统的异同
2016/05/16 Javascript
js的form表单提交url传参数(包含+等特殊字符)的两种解决方法
2016/05/25 Javascript
JS获取子窗口中返回的数据实现方法
2016/05/28 Javascript
Vue实现web分页组件详解
2017/11/28 Javascript
Vue实现搜索 和新闻列表功能简单范例
2018/03/16 Javascript
vue 的keep-alive缓存功能的实现
2018/03/22 Javascript
vue-router懒加载速度缓慢问题及解决方法
2018/11/25 Javascript
mpvue实现左侧导航与右侧内容的联动
2019/10/21 Javascript
关于vue2强制刷新,解决页面不会重新渲染的问题
2019/10/29 Javascript
写了个监控nginx进程的Python脚本
2012/05/10 Python
详解Python中DOM方法的动态性
2015/04/11 Python
Python内建数据结构详解
2016/02/03 Python
tensorflow1.0学习之模型的保存与恢复(Saver)
2018/04/23 Python
如何更换python默认编辑器的背景色
2020/08/10 Python
Myprotein中国网站:欧洲畅销运动营养品牌
2021/02/11 全球购物
一套软件测试笔试题
2014/07/25 面试题
机械专业个人求职自荐信格式
2013/09/21 职场文书
校长创先争优承诺书
2014/08/30 职场文书
医药销售自我评价200字
2014/09/11 职场文书
求职简历自我评价怎么写
2015/03/10 职场文书
2015年学生会纪检部工作总结
2015/03/31 职场文书
导游词之沈阳植物园
2019/11/30 职场文书
Mysql binlog日志文件过大的解决
2021/10/05 MySQL
JavaScript原型链详解
2021/11/07 Javascript