Home  >  Article  >  Backend Development  >  How to convert text files to csv output in php_PHP tutorial

How to convert text files to csv output in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:10:34775browse

How to convert text files to csv output in php,

The example in this article describes the method of converting text files into csv output in php. Share it with everyone for your reference. The specific implementation method is as follows:

This class provides a quick and easy way to convert a fixed-width CSV file. It can be used to perform an iteration using a SplFileObject, making it very efficient. An iterator only knows the current member, and options are provided to specify line characters and Field delimiter ends, This from CSV files. This class is particularly useful if data needs to come from a fixed-width file and be inserted into a database, since most databases support data input from CSV files.

A convenient feature of this class is that fields can be skipped if not needed in the output, and an array of fields is provided, providing a key/value pair, with the primary holding the value offset, or starting the field's status, and the value contains the width, or length of the field, For example. For example, 12="10 is a field that starts at 12 bits and the width, or length of the field, is 10 characters.

The bottom line character of

defaults to "n", but can be set to any character.

The

delimiter defaults to a comma, but can be set to any character, or characters.

The output from the file can be used directly, written to a file, inserted into a database or for any other purpose.

PHP example code is as follows:

Copy code The code is as follows:
/** 
* 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 UsageExample usage
Copy code The code is as follows:
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 ();
}
?>

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/935483.htmlTechArticleHow to convert text files to csv output in php. This article describes the method of converting text files to csv output in php. Share it with everyone for your reference. The specific implementation method is as follows: This class...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn