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 ^