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类
Apr 09 PHP
PHP中10个不常见却非常有用的函数
Mar 21 PHP
PHP中设置时区,记录日志文件的实现代码
Jan 07 PHP
深入PHP curl参数的详解
Jun 17 PHP
phpmyadmin打开很慢的解决方法
Apr 21 PHP
个人写的PHP验证码生成类分享
Aug 21 PHP
PHP中file_get_contents高?用法实例
Sep 24 PHP
PHP 在数组中搜索给定的简单实例 array_search 函数
Jun 13 PHP
thinkphp5 URL和路由的功能详解与实例
Dec 26 PHP
PHP多线程模拟实现秒杀抢单
Feb 07 PHP
ThinkPhP+Apache+PHPstorm整合框架流程图解
Nov 23 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的引用原因分析
2012/09/06 PHP
PHP使用fopen与file_get_contents读取文件实例分享
2016/03/04 PHP
一个可以随意添加多个序列的tag函数
2009/07/21 Javascript
js制作带有遮罩弹出层实现登录注册表单特效代码分享
2015/09/05 Javascript
深入探秘jquery瀑布流的实现
2016/01/30 Javascript
详解升级react-router 4 踩坑指南
2017/08/14 Javascript
ES6关于Promise的用法详解
2018/05/07 Javascript
angularJs中json数据转换与本地存储的实例
2018/10/08 Javascript
微信小程序时间控件picker view使用详解
2018/12/28 Javascript
JS尾递归的实现方法及代码优化技巧
2019/01/19 Javascript
微信小程序顶部导航栏滑动tab效果
2019/01/28 Javascript
Vue如何将页面导出成PDF文件
2020/08/17 Javascript
对于Python异常处理慎用“except:pass”建议
2015/04/02 Python
基于python爬虫数据处理(详解)
2017/06/10 Python
基于python中staticmethod和classmethod的区别(详解)
2017/10/24 Python
python 定义n个变量方法 (变量声明自动化)
2018/11/10 Python
Python实现的ftp服务器功能详解【附源码下载】
2019/06/26 Python
python 实现波浪滤镜特效
2020/12/02 Python
用python计算文件的MD5值
2020/12/23 Python
HTML5验证以及日期显示的实现详解
2013/07/05 HTML / CSS
乌克兰数字设备、配件和智能技术的连锁商店:KTC
2020/08/18 全球购物
出国留学自荐信
2013/10/25 职场文书
实用的简历自我评价
2014/03/06 职场文书
党员干部一句话承诺
2014/05/30 职场文书
政府采购方案
2014/06/12 职场文书
2014年学雷锋活动总结
2014/06/26 职场文书
机械设计专业大学生职业生涯规划书范文
2014/09/13 职场文书
党的群众路线学习笔记
2014/11/06 职场文书
2014年扶贫工作总结
2014/11/18 职场文书
销售2014年度工作总结
2014/12/08 职场文书
2015年元宵节活动总结
2015/02/06 职场文书
大学生简历自我评价2015
2015/03/03 职场文书
2016大学生社会实践单位评语
2015/12/01 职场文书
Python图片处理之图片裁剪教程
2021/05/27 Python
python读取mnist数据集方法案例详解
2021/09/04 Python
OpenCV实现常见的四种图像几何变换
2022/04/01 Python