How PHP converts text files to CSV output _php tips

Source: Internet
Author: User
Tags eol throw exception

This article illustrates how PHP converts a text file to a CSV output. Share to everyone for your reference. The implementation methods are as follows:

This class provides a quick, easy way to convert a fixed-width CSV file, and it can be used 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 end, this splfileobject Files. This class is particularly useful if the data needs to be from a fixed-width file and inserted into the database because most databases support data entry from the CSV file.

This kind of handy feature is the ability to skip fields if not required in the output, the array provided in this field provides a key/value pair, with the main hold of the value offset, or the starting field status, and the value contains the width, or the length of the field, for the example. For example, 12 = "10 is a field, Starts at 12-bit and width or field length of 10 characters.

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

Delimited Fummer 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, or inserted into a database or any other purpose.

The PHP instance code is as follows:

Copy Code code 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, 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, are called when trying to assign a of value to non-existing
*
* @access Public
* @param string $name The name of the property to set
* @param mixed $value The value of
* @throw Excption If 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 are 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 to 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 Sample Usage
Copy Code code 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 =>, =>, => 25);

/*** output the converted lines ***/
foreach ($file as $line)
{
Echo $line;
}

/*** a new instance ***/
$new = new Fixed2csv (' My_file.txt ');

/*** get only and third fields ***/
$new-> fields = Array (0 =>, => 20);
/*** output only the third fields ***/
foreach ($new as $line)
{
Echo $line;
}

}
catch (Exception $e)
{
echo $e-> getMessage ();
}
?>

I hope this article will help you with your PHP program design.

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.