phpテキストファイルをcsv出力に変換する方法

4838 ワード

この例では、phpがテキストファイルをcsv出力に変換する方法について説明します.皆さんの参考にしてください.具体的な実現方法は以下の通りである.
このクラスは固定幅のCSVファイルに変換することを提供して、迅速で、簡便な方法、それはSplFileObjectを反復を実行するために使用することができて、それに非常に効率的な1つの反復は現在のメンバーだけを知っていて、オプションは指定の行の文字とフィールドの区切り記号に提供して終わります、This from CSV files.このクラスは、多くのデータベースがCSVファイルからのデータ入力をサポートするため、データが一定幅のファイルから必要とする場合に特に有用である.
このような便利な機能は、フィールドをスキップすることができる出力の必要がなければ、その領域のアレイが提供し、キー/値ペアを提供し、主に保有する価値とオフセットしたり、領域を起動したりする地位と、値に含まれる幅、またはフィールドの長さ、For exampleを提供することである.例えば、12="10は、12ビットおよび幅またはフィールドの長いが10文字で始まる領域である.
下の行文字はデフォルトでは「n」ですが、任意の文字に設定できます.
区切り記号のデフォルトはカンマですが、任意の文字、または文字に設定できます.
ファイルの出力から直接使用する、1つのファイルに書き込む、データベースまたは他の目的に挿入することができる.
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の使用例

    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プログラム設計に役立つことを願っています.