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 之 没有mysql支持时的替代方案
Oct 09 PHP
PHP 无限分类三种方式 非函数的递归调用!
Aug 26 PHP
php的chr和ord函数实现字符加减乘除运算实现代码
Dec 05 PHP
利用ThinkPHP内置的ThinkAjax实现异步传输技术的实现方法
Dec 19 PHP
那些年一起学习的PHP(二)
Mar 21 PHP
php中将字符串转为HTML的实体引用的一个类
Feb 03 PHP
在PHP中使用X-SendFile头让文件下载更快
Jun 01 PHP
PHP自带函数给数字或字符串自动补齐位数
Jul 29 PHP
PHP在线书签系统分享
Jan 04 PHP
Laravel中使用FormRequest进行表单验证方法及问题汇总
Jun 19 PHP
PHP设计模式之工厂模式详解
Oct 24 PHP
php layui实现前端多图上传实例
Jul 30 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桌面中心(四) 数据显示
2007/03/11 PHP
php实现映射操作实例详解
2019/10/02 PHP
jQuery 1.0.4 - New Wave Javascript(js源文件)
2007/01/15 Javascript
动态加载js的方法汇总
2015/02/13 Javascript
原生javascript实现解析XML文档与字符串
2016/03/01 Javascript
基于BootStrap Metronic开发框架经验小结【二】列表分页处理和插件JSTree的使用
2016/05/12 Javascript
Javascript函数中的arguments.callee用法实例分析
2016/09/16 Javascript
原生JS实现的放大镜效果实例代码
2016/10/15 Javascript
JS匿名函数实例分析
2016/11/26 Javascript
JS实现的集合去重,交集,并集,差集功能示例
2018/03/13 Javascript
超出JavaScript安全整数限制的数字计算BigInt详解
2018/06/24 Javascript
vue3.0 CLI - 2.2 - 组件 home.vue 的初步改造
2018/09/14 Javascript
小程序兼容安卓和IOS数据处理问题及坑
2018/09/18 Javascript
Vuex 单状态库与多模块状态库详解
2018/12/11 Javascript
React 使用Hooks简化受控组件的状态绑定
2019/03/18 Javascript
详解JS实现简单的时分秒倒计时代码
2019/04/25 Javascript
微信小程序开发之左右分栏效果的实例代码
2019/05/20 Javascript
浅析vue-router实现原理及两种模式
2020/02/11 Javascript
[03:01]DOTA2英雄基础教程 露娜
2014/01/07 DOTA
Python xlrd读取excel日期类型的2种方法
2015/04/28 Python
利用python批量给云主机配置安全组的方法教程
2017/06/21 Python
python统计多维数组的行数和列数实例
2018/06/23 Python
如何通过雪花算法用Python实现一个简单的发号器
2019/07/03 Python
python读取.mat文件的数据及实例代码
2019/07/12 Python
基于Python获取照片的GPS位置信息
2020/01/20 Python
python字符串替换re.sub()实例解析
2020/02/09 Python
意大利和国际奢侈品牌购物网站:Suitnegozi.com
2021/01/15 全球购物
应届生文秘专业个人自荐信格式
2013/09/21 职场文书
业务部经理岗位职责
2014/01/04 职场文书
公司开业庆典主持词
2014/03/21 职场文书
难忘的一天教学反思
2014/04/30 职场文书
电工技术比武方案
2014/05/11 职场文书
好的促销活动方案
2014/08/21 职场文书
golang内置函数len的小技巧
2021/07/25 Golang
MySQL数据库完全卸载的方法
2022/03/03 MySQL
Spring Boot 底层原理基础深度解析
2022/04/03 Java/Android