php将文本文件转换csv输出的方法

 更新时间:2014年12月31日 09:26:19   投稿:shichen2014  
这篇文章主要介绍了php将文本文件转换csv输出的方法,通过对SplFileObject类的继承与扩展实现文本文件转换输出的功能,是非常实用的技巧,需要的朋友可以参考下
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

本文实例讲述了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获取表单所有复选框的值的方法

    这篇文章主要介绍了PHP获取表单所有复选框的值的方法,是进行PHP程序设计表单操作中所必须掌握的常用技巧,需要的朋友可以参考下
    2014-08-08
  • PHP强制转化的形式整理

    PHP强制转化的形式整理

    在本篇文章里小编给大家分享的是关于PHP强制转化的形式整理内容,需要的朋友们可以参考下。
    2020-05-05
  • PHP简单实现欧拉函数Euler功能示例

    PHP简单实现欧拉函数Euler功能示例

    这篇文章主要介绍了PHP简单实现欧拉函数Euler功能,简单说明了欧拉函数的概念、原理,并结合实例形式分析了php实现欧拉函数的相关操作技巧,需要的朋友可以参考下
    2017-11-11
  • PHP文件与目录操作示例

    PHP文件与目录操作示例

    这篇文章主要介绍了PHP文件与目录操作,涉及php针对文件与目录的遍历、判断与排序相关操作技巧,注释中备有较为详细的说明,需要的朋友可以参考下
    2016-12-12
  • Ajax+PHP 边学边练之四 表单

    Ajax+PHP 边学边练之四 表单

    通过上一篇文章已经了解到如何利用Ajax和PHP对数据库进行数据读取,这样可以动态的获取到数据库的最新数据。本篇则继续介绍通过表单(Form)向数据库中写入数据。
    2009-11-11
  • PHP实现PDO操作mysql存储过程示例

    PHP实现PDO操作mysql存储过程示例

    这篇文章主要介绍了PHP实现PDO操作mysql存储过程,结合具体实例形式分析了php使用pdo操作mysql存储过程实现用户注册功能相关技巧,需要的朋友可以参考下
    2019-02-02
  • php的SimpleXML方法读写XML接口文件实例解析

    php的SimpleXML方法读写XML接口文件实例解析

    在php5中读写xml文档是非常方便的,可以直接使用php的SimpleXML方法来快速解析与生成xml格式的文件,本文实例说明如下,需要的朋友可以参考下
    2014-06-06
  • 用header 发送cookie的php代码

    用header 发送cookie的php代码

    用header 发送cookie的php代码...
    2007-03-03
  • 用PHP实现小写金额转换大写金额的代码(精确到分)

    用PHP实现小写金额转换大写金额的代码(精确到分)

    数字金额转换成中文大写金额的函数 String Int $num 要转换的小写数字或小写字符串
    2012-01-01
  • php多层数组与对象的转换实例代码

    php多层数组与对象的转换实例代码

    通过json_decode(json_encode($object)可以将对象一次性转换为数组,但是object中遇到非utf-8编码的非ascii字符则会出现问题,比如gbk的中文,何况json_encode和decode的性能也值得疑虑
    2013-08-08

最新评论


http://www.vxiaotou.com