亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2014年12月31日 09:26:19   投稿:shichen2014  
這篇文章主要介紹了php將文本文件轉換csv輸出的方法,通過對SplFileObject類的繼承與擴展實現(xiàn)文本文件轉換輸出的功能,是非常實用的技巧,需要的朋友可以參考下

本文實例講述了php將文本文件轉換csv輸出的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:

這個類提供了轉換成固定寬度的CSV文件,快速,簡便的方法,它可將SplFileObject用于執(zhí)行迭代,使它非常高效的一個迭代只知道當前成員,期權是提供給指定行字符和字段分隔符結束,This from CSV files.這個類是特別有用的,如果數(shù)據(jù)需要來自一個固定寬度的文件,并插入到數(shù)據(jù)庫中,因為大多數(shù)的數(shù)據(jù)庫支持從CSV文件中的數(shù)據(jù)輸入.

這一類的方便的功能是可以跳過字段如果不是在輸出需要,該領域的陣列提供,提供了一個鍵/值對,與主要持有的價值偏移,或啟動領域的地位,和值包含的寬度,或字段的長度,For example.例如,12 =“10是一個領域,在12位和寬度或字段的長度為10個字符開始.

底的行字符默認成“ n”,而是可以設置為任何字符。

分隔符默認為一個逗號,但可以設置為任何字符,或字符。

從文件的輸出可以直接使用,寫入一個文件,到數(shù)據(jù)庫或任何其他目的插入.

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程序設計有所幫助。

相關文章

  • PHP獲取表單所有復選框的值的方法

    PHP獲取表單所有復選框的值的方法

    這篇文章主要介紹了PHP獲取表單所有復選框的值的方法,是進行PHP程序設計表單操作中所必須掌握的常用技巧,需要的朋友可以參考下
    2014-08-08
  • PHP強制轉化的形式整理

    PHP強制轉化的形式整理

    在本篇文章里小編給大家分享的是關于PHP強制轉化的形式整理內容,需要的朋友們可以參考下。
    2020-05-05
  • PHP簡單實現(xiàn)歐拉函數(shù)Euler功能示例

    PHP簡單實現(xiàn)歐拉函數(shù)Euler功能示例

    這篇文章主要介紹了PHP簡單實現(xiàn)歐拉函數(shù)Euler功能,簡單說明了歐拉函數(shù)的概念、原理,并結合實例形式分析了php實現(xiàn)歐拉函數(shù)的相關操作技巧,需要的朋友可以參考下
    2017-11-11
  • PHP文件與目錄操作示例

    PHP文件與目錄操作示例

    這篇文章主要介紹了PHP文件與目錄操作,涉及php針對文件與目錄的遍歷、判斷與排序相關操作技巧,注釋中備有較為詳細的說明,需要的朋友可以參考下
    2016-12-12
  • Ajax+PHP 邊學邊練之四 表單

    Ajax+PHP 邊學邊練之四 表單

    通過上一篇文章已經(jīng)了解到如何利用Ajax和PHP對數(shù)據(jù)庫進行數(shù)據(jù)讀取,這樣可以動態(tài)的獲取到數(shù)據(jù)庫的最新數(shù)據(jù)。本篇則繼續(xù)介紹通過表單(Form)向數(shù)據(jù)庫中寫入數(shù)據(jù)。
    2009-11-11
  • PHP實現(xiàn)PDO操作mysql存儲過程示例

    PHP實現(xiàn)PDO操作mysql存儲過程示例

    這篇文章主要介紹了PHP實現(xiàn)PDO操作mysql存儲過程,結合具體實例形式分析了php使用pdo操作mysql存儲過程實現(xiàn)用戶注冊功能相關技巧,需要的朋友可以參考下
    2019-02-02
  • php的SimpleXML方法讀寫XML接口文件實例解析

    php的SimpleXML方法讀寫XML接口文件實例解析

    在php5中讀寫xml文檔是非常方便的,可以直接使用php的SimpleXML方法來快速解析與生成xml格式的文件,本文實例說明如下,需要的朋友可以參考下
    2014-06-06
  • 用header 發(fā)送cookie的php代碼

    用header 發(fā)送cookie的php代碼

    用header 發(fā)送cookie的php代碼...
    2007-03-03
  • 用PHP實現(xiàn)小寫金額轉換大寫金額的代碼(精確到分)

    用PHP實現(xiàn)小寫金額轉換大寫金額的代碼(精確到分)

    數(shù)字金額轉換成中文大寫金額的函數(shù) String Int $num 要轉換的小寫數(shù)字或小寫字符串
    2012-01-01
  • php多層數(shù)組與對象的轉換實例代碼

    php多層數(shù)組與對象的轉換實例代碼

    通過json_decode(json_encode($object)可以將對象一次性轉換為數(shù)組,但是object中遇到非utf-8編碼的非ascii字符則會出現(xiàn)問題,比如gbk的中文,何況json_encode和decode的性能也值得疑慮
    2013-08-08

最新評論