php將文本文件轉(zhuǎn)換csv輸出的方法
本文實(shí)例講述了php將文本文件轉(zhuǎn)換csv輸出的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
這個(gè)類(lèi)提供了轉(zhuǎn)換成固定寬度的CSV文件,快速,簡(jiǎn)便的方法,它可將SplFileObject用于執(zhí)行迭代,使它非常高效的一個(gè)迭代只知道當(dāng)前成員,期權(quán)是提供給指定行字符和字段分隔符結(jié)束,This from CSV files.這個(gè)類(lèi)是特別有用的,如果數(shù)據(jù)需要來(lái)自一個(gè)固定寬度的文件,并插入到數(shù)據(jù)庫(kù)中,因?yàn)榇蠖鄶?shù)的數(shù)據(jù)庫(kù)支持從CSV文件中的數(shù)據(jù)輸入.
這一類(lèi)的方便的功能是可以跳過(guò)字段如果不是在輸出需要,該領(lǐng)域的陣列提供,提供了一個(gè)鍵/值對(duì),與主要持有的價(jià)值偏移,或啟動(dòng)領(lǐng)域的地位,和值包含的寬度,或字段的長(zhǎng)度,For example.例如,12 =“10是一個(gè)領(lǐng)域,在12位和寬度或字段的長(zhǎng)度為10個(gè)字符開(kāi)始.
底的行字符默認(rèn)成“ n”,而是可以設(shè)置為任何字符。
分隔符默認(rèn)為一個(gè)逗號(hào),但可以設(shè)置為任何字符,或字符。
從文件的輸出可以直接使用,寫(xiě)入一個(gè)文件,到數(shù)據(jù)庫(kù)或任何其他目的插入.
PHP實(shí)例代碼如下:
/**
* 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示例用法
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 ();
}
?>
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
- PHP將HTML轉(zhuǎn)換成文本的實(shí)現(xiàn)代碼
- php中將html中的br換行符轉(zhuǎn)換為文本輸入中的換行符
- 使用PHP+JavaScript將HTML頁(yè)面轉(zhuǎn)換為圖片的實(shí)例分享
- 分享php代碼將360瀏覽器導(dǎo)出的favdb的sqlite數(shù)據(jù)庫(kù)文件轉(zhuǎn)換為html
- php實(shí)現(xiàn)二進(jìn)制和文本相互轉(zhuǎn)換的方法
- php實(shí)現(xiàn)使用正則將文本中的網(wǎng)址轉(zhuǎn)換成鏈接標(biāo)簽
- PHP實(shí)現(xiàn)把文本中的URL轉(zhuǎn)換為鏈接的auolink()函數(shù)分享
- 把文本中的URL地址轉(zhuǎn)換為可點(diǎn)擊鏈接的JavaScript、PHP自定義函數(shù)
- php導(dǎo)出csv格式數(shù)據(jù)并將數(shù)字轉(zhuǎn)換成文本的思路以及代碼分享
- 自動(dòng)把純文本轉(zhuǎn)換成Web頁(yè)面的php代碼
- php實(shí)現(xiàn)轉(zhuǎn)換html格式為文本格式的方法
相關(guān)文章
PHP簡(jiǎn)單實(shí)現(xiàn)歐拉函數(shù)Euler功能示例
這篇文章主要介紹了PHP簡(jiǎn)單實(shí)現(xiàn)歐拉函數(shù)Euler功能,簡(jiǎn)單說(shuō)明了歐拉函數(shù)的概念、原理,并結(jié)合實(shí)例形式分析了php實(shí)現(xiàn)歐拉函數(shù)的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11PHP實(shí)現(xiàn)PDO操作mysql存儲(chǔ)過(guò)程示例
這篇文章主要介紹了PHP實(shí)現(xiàn)PDO操作mysql存儲(chǔ)過(guò)程,結(jié)合具體實(shí)例形式分析了php使用pdo操作mysql存儲(chǔ)過(guò)程實(shí)現(xiàn)用戶注冊(cè)功能相關(guān)技巧,需要的朋友可以參考下2019-02-02php的SimpleXML方法讀寫(xiě)XML接口文件實(shí)例解析
在php5中讀寫(xiě)xml文檔是非常方便的,可以直接使用php的SimpleXML方法來(lái)快速解析與生成xml格式的文件,本文實(shí)例說(shuō)明如下,需要的朋友可以參考下2014-06-06用PHP實(shí)現(xiàn)小寫(xiě)金額轉(zhuǎn)換大寫(xiě)金額的代碼(精確到分)
數(shù)字金額轉(zhuǎn)換成中文大寫(xiě)金額的函數(shù) String Int $num 要轉(zhuǎn)換的小寫(xiě)數(shù)字或小寫(xiě)字符串2012-01-01php多層數(shù)組與對(duì)象的轉(zhuǎn)換實(shí)例代碼
通過(guò)json_decode(json_encode($object)可以將對(duì)象一次性轉(zhuǎn)換為數(shù)組,但是object中遇到非utf-8編碼的非ascii字符則會(huì)出現(xiàn)問(wèn)題,比如gbk的中文,何況json_encode和decode的性能也值得疑慮2013-08-08