class CsvFieldDump {
var $headers;
var $fieldnum;
function CsvFieldDump($infile){
if(empty($infile)){
die("読み取るcsvファイルを指定する必要があります");
}
if(!file_exists($infile)){
die("$infile が存在しません!n");
}
$this->infile = $infile;
$this->fieldnum = $field;
$this->createFile=0;
return(true);
}
var $headers=array();
function dump_headers($dumptoscreen=1){
$this->gt;fd=fopen($this ->infile, "r") または die("CSV ファイル $file を開けませんでした");
$count=0;
while($data = fgetcsv($this->fd, 1000, "," ) ){
if(!$count){
if($dumptoscreen){
print_r($data);
}
$this->headers=$data;
}
$count=1;
}
return($this->headers);
}
function dump_column($num){
$this->fieldnum = $num;
$this->fd=fopen($this- >infile, "r") または die("CSV ファイル $file を開けませんでした");
$count=0;
while($data = fgetcsv($this->fd, 1000, ",") ){
$array=array();
if(is_array($this->fieldnum)){
foreach($this->fieldnum as $num){
$array[]=$data[$num] ;
}
}else{
$array = array($data[$this->fieldnum]);
}
$this->write_line($array);
}
}
var $colname;
var $headers_flip;
function dump_column_by_name($name){
if(!isset($name)){
die("フィールド名が指定されていません");
}
$this->colname=$name;
$ this->dump_headers(0);
$this->headers_flip = array_flip($this->headers);
if(is_array($this->colname)){
foreach($this->colname) as $n){
$array[] = $this->headers_flip[$n];
}
}else{
$array[] = $this->headers_flip[$this->colname];
}
$this->dump_column($array);
}
function write_line($data){
$string="";
$num=count($data);
for($x=0; $x $string.=$data[$x];
if($x $string.=",";
}
}
echo $ string."n";
}
}
////////////////////// C プログラムだと思わせる :)
if(isset ($argv[1])){
$filename = $argv[count($argv)-1];
if($argv[1] == "-h"){
render_help();
die ();
}else{
$b=new CsvFieldDump($filename);
}
$x=0;
foreach($argv as $opt){
switch($opt) {
case "df":
case "-df" :
$number = $argv[$x+1];
if(strstr($number, ",")){
$numbers =explode(",",$number);
} else{
$numbers = $number;
}
$b->dump_column($numbers);
break;
case "-dn":
$number = $argv[$x+1];
if( strstr($number, ",")){
$names =explode(",",$number);
}else{
$names = $number;
}
$b->dump_column_by_name($names);
break;
case "--help":
case "-h" :
render_help();
休憩;
$ b-> dump_headers(); .com
-df
-dn
-dh = CSV ヘッダーをダンプします
";
}
?>
http://www.bkjia.com/PHPjc/445005.html
www.bkjia.com