Php converts text files to csv files

Source: Internet
Author: User

Php converts text files to csv files

Php converts text files to csv files
This class provides a quick and easy way to convert a CSV file to a fixed width. It can use SplFileObject to execute iterations so that an iteration that is very efficient only knows the current member. An option is provided to end with a specified line character and field separator. This from CSV files. This class is particularly 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 type of convenient function can skip fields if not required in the output. The arrays in this field provide a key/value pair, with the primary hold value offset, or startup field status, and the value contains the width, or the length of the field. For example. 12 = "10" is a field that starts with 12 characters in length and width or field.

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.

<? 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 "n ";

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

?>


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.