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

首頁 > 開發 > PHP > 正文

php 將文本文件轉換csv輸出

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

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

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

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

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

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

PHP實例代碼如下:

  1. <?php 
  2.  
  3. /**  
  4. * Class to convert fixed width files into CSV format  
  5. * Allows to set fields, separator, and end-of-line character  
  6.  
  7. * @author Kevin Waterson  
  8. * @url http://phpro.org  
  9. * @version $Id$  
  10.  
  11. */  
  12. class fixed2CSV extends SplFileObject  
  13. {  
  14. /**  
  15.  
  16. * Constructor, duh, calls the parent constructor  
  17.  
  18. * @access       public  
  19. * @param    string  The full path to the file to be converted  
  20.  
  21. */  
  22. public function __construct ( $filename )  
  23. {  
  24. parent :: __construct ( $filename );  
  25.  
  26. /*  
  27. * Settor, is called when trying to assign a value to non-existing property  
  28.  
  29. * @access    public  
  30. * @param    string    $name    The name of the property to set  
  31. * @param    mixed    $value    The value of the property  
  32. * @throw    Excption if property is not able to be set  
  33.  
  34. */  
  35. public function __set ( $name , $value )  
  36. {  
  37. switch$name )  
  38. {  
  39. case 'eol' :  
  40. case 'fields' :  
  41. case 'separator' :  
  42. $this -> $name = $value ;  
  43. break
  44.  
  45. default:  
  46. throw new Exception ( "Unable to set $name " );  
  47. }  
  48.  
  49. /**  
  50.  
  51. * Gettor This is called when trying to access a non-existing property  
  52.  
  53. * @access    public  
  54. * @param    string    $name    The name of the property  
  55. * @throw    Exception if proplerty cannot be set  
  56. * @return    string  
  57.  
  58. */  
  59. public function __get ( $name )  
  60. {  
  61. switch$name )  
  62. {  
  63. case 'eol' :  
  64. return " " ; 
  65.  
  66. case 'fields' :  
  67. return array(); 
  68.  
  69. case 'separator' :  
  70. return ',' ; 
  71.  
  72. default:  
  73. throw new Exception ( " $name cannot be set" );  
  74. }  
  75.  
  76. /**  
  77.  
  78. * Over ride the parent current method and convert the lines  
  79.  
  80. * @access    public  
  81. * @return    string    The line as a CSV representation of the fixed width line, false otherwise  
  82.  
  83. */  
  84. public function current ()  
  85. {  
  86. if( parent :: current () )  
  87. {  
  88. $csv = '' ;  
  89. $fields = new cachingIterator ( new ArrayIterator ( $this -> fields ) );  
  90. foreach$fields as $f )  
  91. {  
  92. $csv .= trim ( substr ( parent :: current (), $fields -> key (), $fields -> current ()  ) );  
  93. $csv .= $fields -> hasNext () ? $this -> separator : $this -> eol ;  
  94. }  
  95. return $csv ;  
  96. }  
  97. return false ;  
  98. }  
  99. // end of class 
  100.  
  101. ?> 
  102.  
  103. Example Usage示例用法 
  104.  
  105. <?php 
  106.  
  107. try  
  108. {  
  109. /*** the fixed width file to convert ***/  
  110. $file = new fixed2CSV ( 'my_file.txt' ); 
  111.  
  112. /*** The start position=>width of each field ***/  
  113. $file -> fields = array( 0 => 10 , 10 => 15 , 25 => 20 , 45 => 25 ); 
  114.  
  115. /*** output the converted lines ***/  
  116. foreach$file as $line )  
  117. {  
  118. echo $line ;  
  119.  
  120. /*** a new instance ***/  
  121. $new = new fixed2CSV ( 'my_file.txt' ); 
  122.  
  123. /*** get only first and third fields ***/  
  124. $new -> fields = array( 0 => 10 , 25 => 20 ); 
  125. //開源代碼Vevb.com 
  126. /*** output only the first and third fields ***/  
  127. foreach$new as $line )  
  128. {  
  129. echo $line ;  
  130.  
  131. }  
  132. catch( Exception $e )  
  133. {  
  134. echo $e -> getMessage ();  
  135.  
  136. ?> 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 长春市| 宣城市| 东海县| 肥城市| 兴国县| 收藏| 离岛区| 普定县| 八宿县| 浮梁县| 东丽区| 汽车| 绵竹市| 瓦房店市| 义马市| 阿克陶县| 沐川县| 东乡族自治县| 信丰县| 凉山| 乐清市| 景宁| 东阿县| 鹿邑县| 伊金霍洛旗| 武义县| 长春市| 武陟县| 沙雅县| 永靖县| 石屏县| 玉龙| 永清县| 长白| 临沂市| 尖扎县| 岚皋县| 鄯善县| 澜沧| 阿荣旗| 澄城县|