源码网,源码论坛,源码之家,商业源码,游戏源码下载,discuz插件,棋牌源码下载,精品源码论坛

 找回密码
 立即注册
楼主: ttx9n

[PHP编程] php将文本文件转换csv输出的方法

[复制链接]

7万

主题

861

回帖

32万

积分

论坛元老

Rank: 8Rank: 8

积分
329525
发表于 2018-12-25 17:47:35 | 显示全部楼层 |阅读模式
这篇文章主要介绍了php将文本文件转换csv输出的方法,通过对SplFileObject类的继承与扩展实现文本文件转换输出的功能,是非常实用的技巧,需要的朋友可以参考下

本文实例讲述了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程序设计有所帮助。

回复

使用道具 举报

2

主题

2万

回帖

2万

积分

论坛元老

Rank: 8Rank: 8

积分
25466
发表于 2022-8-18 19:10:38 | 显示全部楼层
好东西可以可以可以可以
回复 支持 反对

使用道具 举报

3

主题

2万

回帖

2万

积分

论坛元老

Rank: 8Rank: 8

积分
25283
发表于 2022-10-7 01:30:24 | 显示全部楼层
来看看!!!
回复 支持 反对

使用道具 举报

2

主题

2万

回帖

2万

积分

论坛元老

Rank: 8Rank: 8

积分
25466
发表于 2022-11-18 09:44:28 | 显示全部楼层
哈哈哈哈哈哈哈
回复 支持 反对

使用道具 举报

0

主题

2万

回帖

2万

积分

论坛元老

Rank: 8Rank: 8

积分
25735
发表于 2023-1-24 06:56:01 | 显示全部楼层
还可以不错
回复 支持 反对

使用道具 举报

1

主题

2万

回帖

2万

积分

论坛元老

Rank: 8Rank: 8

积分
25550
发表于 2023-5-6 12:10:50 | 显示全部楼层
。。。。。。。。。。。。。。。
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

1万

积分

论坛元老

Rank: 8Rank: 8

积分
18256
发表于 2023-10-10 15:40:13 | 显示全部楼层
源码源码源码源码源码源码源码源码源码源码源码源码源码
回复 支持 反对

使用道具 举报

2

主题

2万

回帖

2万

积分

论坛元老

Rank: 8Rank: 8

积分
24607
发表于 2024-2-7 14:29:03 | 显示全部楼层
撒房产税陈飞飞
回复 支持 反对

使用道具 举报

0

主题

2万

回帖

2万

积分

论坛元老

Rank: 8Rank: 8

积分
24231
发表于 2024-3-25 11:54:39 | 显示全部楼层
哈哈哈哈哈哈哈
回复 支持 反对

使用道具 举报

3

主题

2万

回帖

2万

积分

论坛元老

Rank: 8Rank: 8

积分
25245
发表于 2024-4-2 06:54:03 | 显示全部楼层
了乐趣了去了去了去了去了
TS人妖演出表演服务q3268336102电话13168842816
回复 支持 反对

使用道具 举报

高级模式
B Color Image Link Quote Code Smilies

本版积分规则

手机版|小黑屋|网站地图|源码论坛 ( 海外版 )

GMT+8, 2025-2-11 21:36 , Processed in 0.076541 second(s), 25 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表