当前位置:首页 > PHP教程 > php应用 > 列表

php将文本文件转换csv输出的方法

发布:smiling 来源: PHP粉丝网  添加日期:2021-05-05 13:20:43 浏览: 评论:0 

这篇文章主要介绍了php将文本文件转换csv输出的方法,通过对SplFileObject类的继承与扩展实现文本文件转换输出的功能,是非常实用的技巧,需要的朋友可以参考下

本文实例讲述了php将文本文件转换csv输出的方法。分享给大家供大家参考。具体实现方法如下:

这个类提供了转换成固定宽度的CSV文件,快速,简便的方法,它可将SplFileObject用于执行迭代,使它非常高效的一个迭代只知道当前成员,期权是提供给指定行字符和字段分隔符结束,This from CSV files.这个类是特别有用的,如果数据需要来自一个固定宽度的文件,并插入到数据库中,因为大多数的数据库支持从CSV文件中的数据输入.

这一类的方便的功能是可以跳过字段如果不是在输出需要,该领域的阵列提供,提供了一个键/值对,与主要持有的价值偏移,或启动领域的地位,和值包含的宽度,或字段的长度,For example.例如,12 =“10是一个领域,在12位和宽度或字段的长度为10个字符开始.

底的行字符默认成“ n”,而是可以设置为任何字符。

分隔符默认为一个逗号,但可以设置为任何字符,或字符。

从文件的输出可以直接使用,写入一个文件,到数据库或任何其他目的插入.

PHP实例代码如下:

  1. <?php 
  2. /**  
  3. * Class to convert fixed width files into CSV format  
  4. * Allows to set fields, separator, and end-of-line character  
  5. *  
  6. * @author Kevin Waterson  
  7. * @url http://phpro.org  
  8. * @version $Id$  
  9. *  
  10. */  
  11. class fixed2CSV extends SplFileObject  
  12. {  
  13. /**  
  14. *  
  15. * Constructor, duh, calls the parent constructor  
  16. *  
  17. * @access       public  
  18. * @param    string  The full path to the file to be converted  
  19. *  
  20. */  
  21. public function __construct ( $filename )  
  22. {  
  23. parent :: __construct ( $filename );  
  24. } 
  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. *  
  52. * Gettor This is called when trying to access a non-existing property  
  53. *  
  54. * @access    public  
  55. * @param    string    $name    The name of the property  
  56. * @throw    Exception if proplerty cannot be set  
  57. * @return    string  
  58. *  
  59. */  
  60. public function __get ( $name )  
  61. {  
  62. switch( $name )  
  63. {  
  64. case 'eol' :  
  65. return " " ; 
  66.  
  67. case 'fields' :  
  68. return array(); 
  69.  
  70. case 'separator' :  
  71. return ',' ; 
  72.  
  73. default:  
  74. throw new Exception ( " $name cannot be set" );  
  75. }  
  76. } 
  77.  
  78. /**  
  79. *  
  80. * Over ride the parent current method and convert the lines  
  81. *  
  82. * @access    public  
  83. * @return    string    The line as a CSV representation of the fixed width line, false otherwise  
  84. *  
  85. */  
  86. public function current ()  
  87. {  
  88. if( parent :: current () )  
  89. {  
  90. $csv = '' ;  
  91. $fields = new cachingIterator ( new ArrayIterator ( $this -> fields ) );  
  92. foreach( $fields as $f )  
  93. {  
  94. $csv .= trim ( substr ( parent :: current (), $fields -> key (), $fields -> current ()  ) );  
  95. $csv .= $fields -> hasNext () ? $this -> separator : $this -> eol ;  
  96. }  
  97. return $csv ;  
  98. }  
  99. return false ;  
  100. }  
  101. } // end of class 
  102. ?> 

Example Usage示例用法:

  1. <?php 
  2. try  
  3. {  
  4. /*** the fixed width file to convert ***/  
  5. $file = new fixed2CSV ( 'my_file.txt' ); 
  6.  
  7. /*** The start position=>width of each field ***/  
  8. $file -> fields = array( 0 => 10 , 10 => 15 , 25 => 20 , 45 => 25 ); 
  9.  
  10. /*** output the converted lines ***/  
  11. foreach( $file as $line )  
  12. {  
  13. echo $line ;  
  14. } 
  15.  
  16. /*** a new instance ***/  
  17. $new = new fixed2CSV ( 'my_file.txt' ); 
  18.  
  19. /*** get only first and third fields ***/  
  20. $new -> fields = array( 0 => 10 , 25 => 20 ); 
  21. /*** output only the first and third fields ***/  
  22. foreach( $new as $line )  
  23. {  
  24. echo $line ;  
  25. } 
  26.  
  27. }  
  28. catch( Exception $e )  
  29. {  
  30. echo $e -> getMessage ();  
  31. } 
  32. ?>

Tags: php文件转换csv

分享到: