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
xajax写的留言本
Nov 25 PHP
PHP获取windows登录用户名的方法
Jun 24 PHP
PHP将进程作为守护进程的方法
Mar 19 PHP
PHP安装memcached扩展笔记
May 28 PHP
PHP二维数组排序简单实现方法
Feb 14 PHP
php+jquery+html实现点击不刷新加载更多的实例代码
Aug 12 PHP
php有效防止图片盗用、盗链的两种方法
Nov 01 PHP
PHP中功能强大却很少使用的函数实例小结
Nov 10 PHP
[原创]php正则删除img标签的方法示例
May 27 PHP
PHP类与对象后期静态绑定操作实例详解
Dec 20 PHP
PHP中$GLOBALS与global的区别详解
Mar 21 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与jquery设置和读取cookies
2013/08/08 PHP
php实现遍历文件夹的方法汇总
2017/03/02 PHP
php获取ajax的headers方法与内容实例
2017/12/27 PHP
浅谈Javascript面向对象编程
2011/11/15 Javascript
JS仿iGoogle自定义首页模块拖拽特效的方法
2015/02/13 Javascript
jQuery简单实现遍历数组的方法
2015/04/14 Javascript
JavaScript中用sort()方法对数组元素进行排序的操作
2015/06/09 Javascript
在Node.js中使用HTTP上传文件的方法
2015/06/23 Javascript
jquery实现两边飘浮可关闭的对联广告
2015/11/27 Javascript
下雪了 javascript实现雪花飞舞
2020/08/02 Javascript
让浏览器崩溃的12行JS代码(DoS攻击分析及防御)
2016/10/10 Javascript
jquery 多个radio的click事件实例
2016/12/03 Javascript
浅析上传头像示例及其注意事项
2016/12/14 Javascript
Vue.js:使用Vue-Router 2实现路由功能介绍
2017/02/22 Javascript
vue指令只能输入正数并且只能输入一个小数点的方法
2018/06/08 Javascript
微信小程序模板消息推送的两种实现方式
2019/08/27 Javascript
Angular8 简单表单验证的实现示例
2020/06/03 Javascript
跟老齐学Python之字典,你还记得吗?
2014/09/20 Python
Python实现保证只能运行一个脚本实例
2015/06/24 Python
Python实现时钟显示效果思路详解
2018/04/11 Python
python之super的使用小结
2018/08/13 Python
python的pip安装以及使用教程
2018/09/18 Python
python 美化输出信息的实例
2018/10/15 Python
python 从文件夹抽取图片另存的方法
2018/12/04 Python
bluepy 一款python封装的BLE利器简单介绍
2019/06/25 Python
Python3中urlencode和urldecode的用法详解
2019/07/23 Python
html5 video标签屏蔽右键视频另存为的js代码
2013/11/12 HTML / CSS
为您的家、后院、车库等在线购物:Spreetail
2019/06/17 全球购物
索尼巴西商店:Sony巴西
2019/06/21 全球购物
师范应届毕业生自荐信
2013/11/18 职场文书
给同事的道歉信
2014/01/11 职场文书
应聘编辑自荐信范文
2014/03/12 职场文书
2015年度房地产工作总结
2015/04/09 职场文书
会计专业自荐信范文
2019/05/22 职场文书
sql中mod()函数取余数的用法
2021/05/29 SQL Server
html5+实现plus.io进行拍照和图片等获取
2022/06/01 HTML / CSS