Compdigitec Labs

« | Home | »

Parsing text files in PHP with FlatfileTools

By admin | August 13, 2008

Compdigitec FlatfileTools is a API written in PHP to manipulate, read and modify plain text databases. It can also manipulate existing databases, such as CSV files. It is released under the GNU General Public License and therefore is free software.

To use it, download Compdigitec FlatfileTools. Then include_once it like this:

<?php

include_once(‘flatfiletools.lib.php’);

// The rest of your code goes here…

?>

For example, to parse the following CSV file – name it testdb.csv. Make sure there is no extra newline at the bottom. (UNIX line endings. To parse Windows/DOS line endings, change line in config.ini to “\r\n”). Do not forget to change config.ini’s div setting to “,”:

Software,License
Linux,GPL
Mozilla Firefox,GPL+LGPL+MPL
Compdigitec FlatfileTools,GPL

Use the following code:


<?php
// Include flatfiletools
include_once('flatfiletools.lib.php');
// Create new db and set file
$f = new FlatfileTools();
$f->set_datafile('testdb.csv');
// Populate - WARNING: Due to a bug in FlatfileTools v1.0, true means failure and false means success in this function.
if($f->populate()) {
    echo 
"An error happened on line 8 during a call to populate().\n";
};
// Get number of lines
$lines $f->get_number_of_lines();
// Output table header
echo "<table border=\"1\">";
// Until the last line get line
for($i 0$i <= $lines$i++) {
    
$var $f->get_line($i);
    if(
$var === false) {
        echo 
"An error happened on line 16 during a call to get_line() - Current Row Number is $i. \n";
    } else {
        echo 
"<tr>";
        if(
$i == 0) {
            foreach(
$var as $val) {
                echo 
"<th>$val</th>";
            }
        } else {
            foreach(
$var as $val) {
                echo 
"<td>$val</td>";
            }
        };
        echo 
"</tr>";
    };
}
// Output footer
echo "</table>";
?>


If you run this code and receive the error “An error happened on line 16 during a call to get_line() – Current Row Number is 5. ” , it’s because your CSV file has an extra newline at the bottom of it. You can supress this error by commenting out the error on line 18.

So as you can see, Compdigitec FlatfileTools is an powerful API to manipulate, parse and read flat text files in PHP. The example above could have easily been customized to manipulate tab delimited text files by changing config.ini’s div to “\t” instead. You can also write to text files with this API – read the full documentation for more details.

If you found this article interesting, please help us spread the word or please leave a comment. You can also subscribe to Compdigitec Labs via RSS.

If you found this article helpful or interesting, please help Compdigitec spread the word. Don’t forget to subscribe to Compdigitec Labs for more useful and interesting articles!

Topics: Internet, Linux, PHP, Windows | 9 Comments »

9 Responses to “Parsing text files in PHP with FlatfileTools”

  1. Aliases for common command line typos at Compdigitec Labs Says:
    August 24th, 2008 at 20:08

    […] one word for each typo) and parse it with Compdigitec FlatfileTools. If you don’t know how, read the article on it. Note: both the alias list and the CSV version use UNIX/Linux encoding […]

  2. ที่พักพูลวิลล่าพัทยา Says:
    July 6th, 2024 at 20:24

    … [Trackback]

    […] There you will find 16241 additional Information on that Topic: compdigitec.com/labs/2008/08/13/parsing-text-files-in-php-with-flatfiletools/ […]

  3. autodetailing Says:
    August 13th, 2024 at 00:57

    … [Trackback]

    […] There you can find 51287 additional Info on that Topic: compdigitec.com/labs/2008/08/13/parsing-text-files-in-php-with-flatfiletools/ […]

  4. สั่งของจากจีน Says:
    August 23rd, 2024 at 21:06

    … [Trackback]

    […] Information on that Topic: compdigitec.com/labs/2008/08/13/parsing-text-files-in-php-with-flatfiletools/ […]

  5. ลวดสลิง Says:
    August 28th, 2024 at 22:09

    … [Trackback]

    […] There you will find 47058 more Information on that Topic: compdigitec.com/labs/2008/08/13/parsing-text-files-in-php-with-flatfiletools/ […]

  6. luggage storage bangkok Says:
    September 11th, 2024 at 20:53

    … [Trackback]

    […] Find More on to that Topic: compdigitec.com/labs/2008/08/13/parsing-text-files-in-php-with-flatfiletools/ […]

  7. luk666 Says:
    September 19th, 2024 at 19:40

    … [Trackback]

    […] Read More on on that Topic: compdigitec.com/labs/2008/08/13/parsing-text-files-in-php-with-flatfiletools/ […]

  8. soft aimbot overwatch Says:
    September 26th, 2024 at 02:38

    … [Trackback]

    […] Read More Info here on that Topic: compdigitec.com/labs/2008/08/13/parsing-text-files-in-php-with-flatfiletools/ […]

  9. Patio Covers in Leander Says:
    October 1st, 2024 at 01:55

    … [Trackback]

    […] There you can find 25409 additional Info on that Topic: compdigitec.com/labs/2008/08/13/parsing-text-files-in-php-with-flatfiletools/ […]

Comments