国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php將文本文件轉換csv輸出的方法

2024-05-04 21:55:52
字體:
來源:轉載
供稿:網友

 這篇文章主要介紹了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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 安阳县| 新巴尔虎左旗| 石嘴山市| 抚顺市| 越西县| 醴陵市| 集安市| 阳江市| 博客| 绵阳市| 织金县| 紫云| 桦南县| 荆门市| 当雄县| 金寨县| 土默特右旗| 获嘉县| 玛多县| 绥中县| 卢湾区| 工布江达县| 台湾省| 凤凰县| 乌恰县| 大荔县| 甘南县| 克东县| 西城区| 阜阳市| 大港区| 屯昌县| 利辛县| 盐池县| 叙永县| 土默特左旗| 翁牛特旗| 定州市| 光泽县| 溆浦县| 志丹县|