PHP Convert text file to CSV output _php tutorial

Source: Internet
Author: User
Tags eol throw exception
PHP Convert text file to CSV output

PHP Convert text file to CSV output
This class provides a quick and easy way to convert a CSV file into a fixed width. It can use Splfileobject to perform iterations, making it very efficient for an iteration to know only the current member. The option is provided to the specified line character and the field delimiter ends. This is a from CSV file. This class is particularly useful if the data needs to come from a fixed-width file and is inserted into the database, because most databases support data entry from the CSV files.

The handy feature of this class is the ability to skip fields if not required in the output. The domain array is provided with a key/value pair, with the main holding value offset, or the status of the boot field, and the value contains the width, or the length of the field. For example. 12 = "10 is a field that starts at 12 bits and width or the length of a field is 10 characters.

The bottom line character defaults to "N", but can be set to any character.

Separate defaults think of a comma, but can be set to any character, or character.

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

/**
* Class to convert fixed width files into CSV format
* Allows to set fields, separator, and End-of-line cha Racter
*
* @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 was 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 if 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));
foreach ($fields as $f)
{
$csv. = Trim (substr (Parent:: Current (), $fields-key (), $fields-current ()));
$csv. = $fields Hasnext ()? $this-Separator: EOL, $this;
}
return $csv;
}
return false;
}
}//End of class

?>

Example Usage Example usages

Try
{
/*** The fixed width file to convert ***/
$file = new Fixed2csv (' My_file.txt ');

/*** the start position=>width of each field ***/
$file, fields = Array (0 = ten, ten, 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 = 20);

/*** output only the first and third fields ***/
foreach ($new as $line)
{
Echo $line;
}

}
catch (Exception $e)
{
Echo $e-getMessage ();
}

?>


http://www.bkjia.com/PHPjc/444982.html www.bkjia.com true http://www.bkjia.com/PHPjc/444982.html techarticle php Convert text file to CSV output PHP convert text file to CSV output This class provides a quick and easy way to convert to a fixed-width csv file. It can be used to splfileobject ...

  • 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.