PHP How to convert a text file to CSV output _php tutorial

Source: Internet
Author: User
Tags eol throw exception

How PHP converts a text file to CSV output


This article mainly introduces PHP to convert text files CSV output method, through the inheritance and extension of the Splfileobject class to achieve the function of text file conversion output, is very practical skills, the need for friends can refer to the next

This example describes how PHP converts a text file to CSV output. Share to everyone for your reference. The implementation method is as follows:

This class provides a fast, easy way to convert to a fixed-width CSV file, which can be used by Splfileobject to perform iterations, making it very efficient for an iteration that only knows the current member, the option is provided to the specified line character and the field delimiter ends, this from CSV Files. 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 a CSV file.

The handy feature of this class is that you can skip the field if it is not required in the output, the field array is provided with a key/value pair, offset with the value of the primary hold, or the status of the boot field, and the value contains the width, or the length of the field, for example. 12 = "10 is an area, The length of the 12-bit and width or field begins with 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 the file can be directly used, written to a file, inserted into a database or any other purpose.

The PHP instance code is as follows:

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

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

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

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/975884.html www.bkjia.com true http://www.bkjia.com/PHPjc/975884.html techarticle How PHP converts a text file to CSV output this article mainly introduces PHP to convert text files CSV output method, through the inheritance and extension of the Splfileobject class to achieve text file conversion and transmission ...

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