Php converts text files to csv files

Source: Internet
Author: User
Tags eol
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, Thisf... 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:

 $ 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's Eparator ': 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
 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 () ;}?>


Tutorial address:

Reprinted! But please include the article address ^

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.