{"id":19,"date":"2008-08-13T13:00:22","date_gmt":"2008-08-13T17:00:22","guid":{"rendered":"http:\/\/www.compdigitec.com\/labs\/?p=19"},"modified":"2008-08-13T13:01:27","modified_gmt":"2008-08-13T17:01:27","slug":"parsing-text-files-in-php-with-flatfiletools","status":"publish","type":"post","link":"http:\/\/www.compdigitec.com\/labs\/2008\/08\/13\/parsing-text-files-in-php-with-flatfiletools\/","title":{"rendered":"Parsing text files in PHP with FlatfileTools"},"content":{"rendered":"<p><a href=\"\/apps\/flatfiletools\">Compdigitec FlatfileTools<\/a> 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.<\/p>\n<p>To use it, download <a href=\"\/downloads\/?file=fftools_1.zip\">Compdigitec FlatfileTools<\/a>. Then <a href=\"http:\/\/php.net\/include_once\">include_once<\/a> it like this:<\/p>\n<p><code><span style=\"color: #000000;\"><span style=\"color: #0000bb;\">&lt;?php<\/span><\/span><\/code><\/p>\n<p><span style=\"color: #007700;\">include_once(<\/span><span style=\"color: #dd0000;\">&#8216;flatfiletools.lib.php&#8217;<\/span><span style=\"color: #007700;\">);<\/span><\/p>\n<p><span style=\"color: #ff8000;\">\/\/\u00a0The\u00a0rest\u00a0of\u00a0your\u00a0code\u00a0goes\u00a0here&#8230;<\/span><\/p>\n<p><span style=\"color: #0000bb;\">?&gt;<br \/>\n<\/span><\/p>\n<p>For example, to parse the following <a rel=\"nofollow\" href=\"http:\/\/en.wikipedia.org\/wiki\/Comma-separated_values\">CSV<\/a> file &#8211; 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 &#8220;\\r\\n&#8221;). <strong>Do not forget to change config.ini&#8217;s div setting to &#8220;,&#8221;:<br \/>\n<\/strong><\/p>\n<ul>\n<li>From <strong><span style=\"text-decoration: line-through;\">div = &#8220;|&#8221;<\/span><\/strong><\/li>\n<li>To <strong>div = &#8220;,&#8221;<\/strong><\/li>\n<\/ul>\n<p><code>Software,License<br \/>\nLinux,GPL<br \/>\nMozilla Firefox,GPL+LGPL+MPL<br \/>\nCompdigitec FlatfileTools,GPL<br \/>\n<\/code><\/p>\n<p>Use the following code:<\/p>\n<p><code><span style=\"color: #000000\"><br \/>\n<span style=\"color: #0000BB\">&lt;?php<br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Include&nbsp;flatfiletools<br \/><\/span><span style=\"color: #007700\">include_once(<\/span><span style=\"color: #DD0000\">'flatfiletools.lib.php'<\/span><span style=\"color: #007700\">);<br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Create&nbsp;new&nbsp;db&nbsp;and&nbsp;set&nbsp;file<br \/><\/span><span style=\"color: #0000BB\">$f&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;new&nbsp;<\/span><span style=\"color: #0000BB\">FlatfileTools<\/span><span style=\"color: #007700\">();<br \/><\/span><span style=\"color: #0000BB\">$f<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">set_datafile<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'testdb.csv'<\/span><span style=\"color: #007700\">);<br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Populate&nbsp;-&nbsp;WARNING:&nbsp;Due&nbsp;to&nbsp;a&nbsp;bug&nbsp;in&nbsp;FlatfileTools&nbsp;v1.0,&nbsp;true&nbsp;means&nbsp;failure&nbsp;and&nbsp;false&nbsp;means&nbsp;success&nbsp;in&nbsp;this&nbsp;function.<br \/><\/span><span style=\"color: #007700\">if(<\/span><span style=\"color: #0000BB\">$f<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">populate<\/span><span style=\"color: #007700\">())&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">\"An&nbsp;error&nbsp;happened&nbsp;on&nbsp;line&nbsp;8&nbsp;during&nbsp;a&nbsp;call&nbsp;to&nbsp;populate().\\n\"<\/span><span style=\"color: #007700\">;<br \/>};<br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Get&nbsp;number&nbsp;of&nbsp;lines<br \/><\/span><span style=\"color: #0000BB\">$lines&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$f<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">get_number_of_lines<\/span><span style=\"color: #007700\">();<br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Output&nbsp;table&nbsp;header<br \/><\/span><span style=\"color: #007700\">echo&nbsp;<\/span><span style=\"color: #DD0000\">\"&lt;table&nbsp;border=\\\"1\\\"&gt;\"<\/span><span style=\"color: #007700\">;<br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Until&nbsp;the&nbsp;last&nbsp;line&nbsp;get&nbsp;line<br \/><\/span><span style=\"color: #007700\">for(<\/span><span style=\"color: #0000BB\">$i&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">0<\/span><span style=\"color: #007700\">;&nbsp;<\/span><span style=\"color: #0000BB\">$i&nbsp;<\/span><span style=\"color: #007700\">&lt;=&nbsp;<\/span><span style=\"color: #0000BB\">$lines<\/span><span style=\"color: #007700\">;&nbsp;<\/span><span style=\"color: #0000BB\">$i<\/span><span style=\"color: #007700\">++)&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$var&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$f<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">get_line<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$i<\/span><span style=\"color: #007700\">);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;if(<\/span><span style=\"color: #0000BB\">$var&nbsp;<\/span><span style=\"color: #007700\">===&nbsp;<\/span><span style=\"color: #0000BB\">false<\/span><span style=\"color: #007700\">)&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">\"An&nbsp;error&nbsp;happened&nbsp;on&nbsp;line&nbsp;16&nbsp;during&nbsp;a&nbsp;call&nbsp;to&nbsp;get_line()&nbsp;-&nbsp;Current&nbsp;Row&nbsp;Number&nbsp;is&nbsp;$i.&nbsp;\\n\"<\/span><span style=\"color: #007700\">;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">\"&lt;tr&gt;\"<\/span><span style=\"color: #007700\">;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(<\/span><span style=\"color: #0000BB\">$i&nbsp;<\/span><span style=\"color: #007700\">==&nbsp;<\/span><span style=\"color: #0000BB\">0<\/span><span style=\"color: #007700\">)&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach(<\/span><span style=\"color: #0000BB\">$var&nbsp;<\/span><span style=\"color: #007700\">as&nbsp;<\/span><span style=\"color: #0000BB\">$val<\/span><span style=\"color: #007700\">)&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">\"&lt;th&gt;$val&lt;\/th&gt;\"<\/span><span style=\"color: #007700\">;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach(<\/span><span style=\"color: #0000BB\">$var&nbsp;<\/span><span style=\"color: #007700\">as&nbsp;<\/span><span style=\"color: #0000BB\">$val<\/span><span style=\"color: #007700\">)&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">\"&lt;td&gt;$val&lt;\/td&gt;\"<\/span><span style=\"color: #007700\">;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">\"&lt;\/tr&gt;\"<\/span><span style=\"color: #007700\">;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;};<br \/>}<br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Output&nbsp;footer<br \/><\/span><span style=\"color: #007700\">echo&nbsp;<\/span><span style=\"color: #DD0000\">\"&lt;\/table&gt;\"<\/span><span style=\"color: #007700\">;<br \/><\/span><span style=\"color: #0000BB\">?&gt;<br \/><\/span><br \/>\n<\/span><br \/>\n<\/code><\/p>\n<p>If you run this code and receive the error &#8220;<strong>An error happened on line 16 during a call to get_line() &#8211; Current Row Number is 5. <\/strong>&#8221; , it&#8217;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.<\/p>\n<p>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&#8217;s div to &#8220;\\t&#8221; instead. You can also write to text files with this API &#8211; <a href=\"\/apps\/flatfiletools\/documentation\/FlatfileTools\/FlatfileTools.html\">read the full documentation<\/a> for more details.<\/p>\n<p>If you found this article interesting, please help us spread the word or please leave a comment. You can also <a href=\"\/labs\/feed\/\">subscribe<\/a> to <a href=\"\/labs\">Compdigitec Labs<\/a> via RSS.<\/p>","protected":false},"excerpt":{"rendered":"<p>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: &lt;?php include_once(&#8216;flatfiletools.lib.php&#8217;); \/\/\u00a0The\u00a0rest\u00a0of\u00a0your\u00a0code\u00a0goes\u00a0here&#8230; ?&gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[14,3,5,4],"tags":[56,55,54,57,864,59,58],"_links":{"self":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/posts\/19"}],"collection":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/comments?post=19"}],"version-history":[{"count":0,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/posts\/19\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/media?parent=19"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/categories?post=19"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/tags?post=19"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}