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.MVC的模板标签系统(二)
Sep 05 PHP
《PHP编程最快明白》第八讲:php启发和小结
Nov 01 PHP
PHP header函数分析详解
Aug 06 PHP
ThinkPHP CURD方法之table方法详解
Jun 18 PHP
php5.3不能连接mssql数据库的解决方法
Dec 27 PHP
php使用fputcsv()函数csv文件读写数据的方法
Jan 06 PHP
php中数据库连接方式pdo和mysqli对比分析
Feb 25 PHP
Yii使用migrate命令执行sql语句的方法
Mar 15 PHP
PHP模块化安装教程
Jun 01 PHP
PHP 文件上传后端处理实用技巧方法
Jan 06 PHP
PHP后期静态绑定实例浅析
Dec 21 PHP
PHP SESSION机制的理解与实例
Mar 22 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生成excel列名超过26列大于Z时的解决方法
2014/12/29 PHP
laravel如何开启跨域功能示例详解
2017/08/31 PHP
php实现简易计算器
2020/08/28 PHP
javascript中的prototype属性实例分析说明
2010/08/09 Javascript
JavaScript DOM 编程艺术(第2版)读书笔记(JavaScript的最佳实践)
2013/10/01 Javascript
JQueryiframe页面操作父页面中的元素与方法(实例讲解)
2013/11/19 Javascript
jQuery select表单提交省市区城市三级联动核心代码
2014/06/09 Javascript
js实现禁止中文输入的方法
2015/01/14 Javascript
jQuery+ajax简单实现文件上传的方法
2016/06/03 Javascript
jQuery简单设置文本框回车事件的方法
2016/08/01 Javascript
jqGrid翻页时数据选中丢失问题的解决办法
2017/02/13 Javascript
layui框架table 数据表格的方法级渲染详解
2018/08/19 Javascript
利用d3.js实现蜂巢图表带动画效果
2019/09/03 Javascript
element-ui如何防止重复提交的方法步骤
2019/12/09 Javascript
python网络编程学习笔记(七):HTML和XHTML解析(HTMLParser、BeautifulSoup)
2014/06/09 Python
Django自定义manage命令实例代码
2018/02/11 Python
Python 实现交换矩阵的行示例
2019/06/26 Python
Python传递参数的多种方式(小结)
2019/09/18 Python
Python3直接爬取图片URL并保存示例
2019/12/18 Python
Python基础之函数原理与应用实例详解
2020/01/03 Python
python使用python-pptx删除ppt某页实例
2020/02/14 Python
python实现PDF中表格转化为Excel的方法
2020/06/16 Python
python 对象真假值的实例(哪些视为False)
2020/12/11 Python
python Zmail模块简介与使用示例
2020/12/19 Python
html5中地理位置定位api接口开发应用小结
2013/01/04 HTML / CSS
印度尼西亚最大的电商平台:Tokopedia(印尼版淘宝)
2017/12/02 全球购物
俄罗斯购买自行车网站:Vamvelosiped
2021/01/29 全球购物
物理教育专业毕业生推荐信
2013/11/03 职场文书
少年闰土教学反思
2014/02/22 职场文书
2014年高考决心书
2014/03/11 职场文书
聚美优品的广告词
2014/03/14 职场文书
产品质量承诺书
2014/03/27 职场文书
股东合作协议书
2014/09/12 职场文书
社区党员干部承诺书
2015/05/04 职场文书
centos7安装mysql5.7经验记录
2022/05/02 Servers
二维码条形码生成的JavaScript脚本库
2022/07/07 Javascript