How php converts text files to csv files

Source: Internet
Author: User
Tags eol throw exception
This article mainly introduces how php converts text files to csv files. it is a very practical technique to implement the function of converting text files to output files through inheritance and extension of the SplFileObject class, for more information about how php converts text files to csv files, see the following example. Share it with you for your reference. The specific implementation method is as follows:

This class provides a fast and easy way to convert a CSV file to a fixed width. it can use SplFileObject for iteration, so that it can only know the current member for an iteration that is very efficient, the option is provided to end with the specified line character and field separator, This from CSV files. this class is especially useful if the data needs to come from a fixed-width file and be inserted into the database, because most databases support data input from CSV files.

This kind of convenient function is to skip the field if it is not required in the output. the array in this field provides a key/value pair, which is offset from the value mainly held, or the position of the startup domain, the width of the value, or the length of the field, For example. for example, 12 = "10 is a field, and the length of the 12-bit and width or field starts with 10 characters.

The row character at the bottom is "n" by default, but can be set to any character.

The delimiter is a comma by default, but can be set to any character or character.

You can directly use the file output to write a file to the database or insert it for any other purpose.

The PHP instance code is as follows:

The code is as follows:

<? 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, callthe 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

The code is as follows:

<? 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 ();
}
?>

I hope this article will help you with php programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.