• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

php 搞代码 4年前 (2022-01-25) 20次浏览 已收录 0个评论

本文实例讲述了php将文本文件转换csv输出的方法。分享给大家供大家参考。具体实现方法如下:

这个类提供了转换成固定宽度的CSV文件,快速,简便的方法,它可将SplFileObject用于执行迭代,使它非常高效的一个迭代只知道当前成员,期权是提供给指定行字符和字段分隔符结束,This from CSV files.这个类是特别有用的,如果数据需要来自一个固定宽度的文件,并插入到数据库中,因为大多数的数据库支持从CSV文件中的数据输入.

这一类的方便的功能是可以跳过字段如果不是在输出需要,该领域的阵列提供,提供了一个键/值对,与主要持有的价值偏移,或启动领域的地位,和值包含的宽度,或字段的长度,For example.例如,12 =“10是一个领域,在12位和宽度或字段的长度为10个字符开始.

底的行字符默认成“ n”,而是可以设置为任何字符。

分隔符默认为一个逗号,但可以设置为任何字符,或字符。

从文件的输出可以直接使用,写入一个文件,到数据库或任何其他目的插入.

PHP实例代码如下:

<?php <br />/**  <br />* Class to convert fixed width files into CSV format  <br />* Allows to set fields, separator, and end-of-line character  <br />*  <br />* @author Kevin Waterson  <br />* @url http://phpro.org  <br />* @version $Id$  <br />*  <br />*/  <br />class fixed2CSV extends SplFileObject  <br />{  <br />/**  <br />*  <br />* Constructor, duh, calls the parent constructor  <br />*  <br />* @access       public  <br />* @param    string  The full path to the file to be converted  <br />*  <br />*/  <br />public function __construct ( $filename )  <br />{  <br />parent :: __construct ( $filename );  <br />} <br /> <br />/*  <br />* Settor, is called when trying to assign a value to non-existing property  <br />*  <br />* @access    public  <br />* @param    string    $name    The name of the property to set  <br />* @param    mixed    $value    The value of the property  <br />* @throw    Excption if property is not able to be set  <br />*  <br />*/  <br />public function __set ( $name , $value )  <br />{  <br />switch( $name )  <br />{  <br />case 'eol' :  <br />case 'fields' :  <br />case 'separator' :  <br />$this -> $name = $value ;  <br />br<div>本文#来源gaodai.ma#com搞##代!^码7网</div><pre>搞代gaodaima码

eak;

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 <br />try  <br />{  <br />/*** the fixed width file to convert ***/  <br />$file = new fixed2CSV ( 'my_file.txt' ); <br /> <br />/*** The start position=>width of each field ***/  <br />$file -> fields = array( 0 => 10 , 10 => 15 , 25 => 20 , 45 => 25 ); <br /> <br />/*** output the converted lines ***/  <br />foreach( $file as $line )  <br />{  <br />echo $line ;  <br />} <br /> <br />/*** a new instance ***/  <br />$new = new fixed2CSV ( 'my_file.txt' ); <br /> <br />/*** get only first and third fields ***/  <br />$new -> fields = array( 0 => 10 , 25 => 20 ); <br />/*** output only the first and third fields ***/  <br />foreach( $new as $line )  <br />{  <br />echo $line ;  <br />} <br /> <br />}  <br />catch( Exception $e )  <br />{  <br />echo $e -> getMessage ();  <br />} <br />?>

希望本文所述对大家的php程序设计有所帮助。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php将文本文件转换csv输出的方法_PHP

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址