Parse A Tab-delimited .TXT File Into An Array With File(); Function?

May 29, 2009

I have a tab-delimited .TXT file that I'm trying to process and insert into a MySQL database. The file() function will write each row of the text file as a separate element in an array. So, my text file (see attached) is pretty simple and only has 2 rows... the first row contains the column headers, i.e. "Column A, Column B, Column C..." and the second row contains the values. i.e. "Value A, Value B, Value C..."

However, I'm running into a problem because several of the columns actually contain multiple values separated by carriage returns (line breaks), and the file() function doesn't seem to like this:
| Column C |
| > Value C1 |
| > Value C2 |
| > Value C3 |

Here's my code:

// Read each row into an array as separate elements
$Rows_array = file(MyText.txt);
// Output the column headers


So, it seems like the file() function does what it's supposed to until it gets to the first line break. Also, a doublequote (") appears before the first value in the column with the carriage return, so I'm sure there's some sort of escaping issue.

Note: I tried file_get_contents() as well to try and get the row into a string instead of an array, and it gives the same result. Ultimately, I AM just going to throw out the line breaks and explode() the values in Column C as their own array or string, but I need to get all those values passed to my PHP code first, which is where I'm struggling. And, I really want to avoid "pre-processing" the txt file by opening it in excel or something and removing the carriage returns in the culprit columns.

View 1 Replies


File Parsing Function For Tab Or Comma Delimited Files

I need a way to parse a file based on the delimitation of either tab or comma. Does anyone know the best way to do this that is the easiest? Also the file I am parsing might have a few empty strings between delimitation. I know about preg_split so if this is the only way could someone provide a link that explains regular expressions because i dont get it.

View 5 Replies

Find Out If Csv File Fields Are Tab Delimited Or Comma Delimited?

how to find out if csv file fields are tab delimited or comma delimited. I need php validation for this.

View 3 Replies

Read In A Cvs Comma Delimited File Into An Array?

I'm stuck. I want to read in a cvs comma delimited file into an array.

The data looks like this
1, 1,10
1, 2, 9
1, 3, 8
1, 4, 7
1, 5, 6

when I print the array out its first entry is 1,2,9 instead of 1,1,10. print_r verifies that 1,1,10 is there--so something must be wrong with my for loop (I also tried a while loop, same problem) but I'll be darn if I can figure out where the problem is.

Here's the code

PHP Code:
$thisPass = 0;
if (($handle = fopen("10Teams.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);


View 6 Replies

Make A Tab Delimited File From Array Or Object?

so I have this object and I want to make a tab delimited text file from it.

here's what I have so far.

function create_tab_file_from_object($the_object)
$string = '';
foreach($the_object as $val)


when i run this i get a text file that looks something like this field1 field2 field3 ... and so on.

How do I get php to make an actual tab delimited file I can open in notepad or excel or whatever..

View 1 Replies

Parse A File That Contains A Function Before?

I have INI files in the form of:


And other PHP files in the form of:


In the previous as you can see the name of the INI files will always be foldername_phpfilename.ini.Now, for example, in the file registration.php at the moment I have this

PHP Code:
$lang_values = parse_ini_file('root/languages/en/regform_registration.ini');

And what I am trying to do is have a function that runs everytime a file is requested like if someone calls the file registration.php a function will run and parse the values in the ini file to have them available when parsing the file

View 1 Replies

Global Variables For The Words From The File And A Function That Will Load The File Into The Array?

I'm writing a script that will select a random word from among words in an input file, multiple times. Now calling file() multiple times seems inefficient, so I'm thinking of having a global array for the words from the file and a function that will load the file into the array (called before selecting random words). Why doesn't it work?

global $words;

function init_words($file)
$words = file($file);
$count = count($words);

View 5 Replies

Parse The XML File Into An Array?

I need to parse the below XML file into a PHP array.

<?xml version="1.0" encoding="utf-16"?>
<xs:schema id="Schedule_x0020_Set" xmlns="" xmlns:xs="" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Schedule_x0020_Set" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">


View 3 Replies

Tell If A File Is Tab Delimited Or CSV?

Is there ANY way to tell if a file is tab delimited or CSV?

I need to offer parsing of both file types but am having trouble writing a parser that works for both.

View 1 Replies

Function Defined In Include File - Parse Error

I created a function to handle the connection to the database and put it in an include file so I wouldn't have to keep repeating that code in the various files connected to a system I'm creating and to make it easier to change the values of the database variable and password variable. I can't get it to work though and keep getting a parse error on "line 2" which is the function defining line.

Is there something about defining a function in an include file that I don't know. I'll list the code of the entire include file below. Everything except for the function defining line is an exact duplicate of code that was already working before. Code:

View 1 Replies

Parse A Multilevel Array To View-file?

I need to parse a multilevel array to my view file.

My array could be like this:

$test = array(
1 => array(
10 => array('text' => 'test'),
15 => array(


My problem is, that this will only show the first level - I want to have unlimited levels.. Is that possible without making a workaround, where I create the HTML in the PHP-file and then passes the HTML to the view file?

View 1 Replies

Comma Delimited File

I am trying to read comma delimited rows of text. The problem is that
some fields may be encapsulated in "" - particularly the text fields but
not numeric fields.

Is there a simple efficient way to parse the fields with comma but also
strip off the "" encapsulating some of the fields with php?

The problem is the "" encapsulation is optional. Some fields will have
it, some won't.

View 3 Replies

Delimiting A Non-delimited File

I have a text file, with addresses of business' that I would like to convert to a more useable format ie CSV. Currently there is no delimiting of the various address components.

"A 1 AUTO SALES 1224 S BROADWAY ST WICHITA, KS 67211-3124 316-263-8748"
"ABERLE FORD INC 1025 MAIN ST SABETHA, KS 66534-1893 913-284-3122"

As you can see from the examples above the only consistent items are the state and the phone number, and the city is always followed by a comma. I am new to regex and the whole range of string handling functions and I am not sure where to start.

View 5 Replies

Use Fgetcsv For Tab Delimited File?

Is it possible to use fgetcsv in PHP to open a tab-delimited file?

View 2 Replies

Parse An Excel File With Embedded Images Into Array?

I have an excel spreadsheet with images which i need to upload to a db. How do i parse the spreadsheet into an array?

View 2 Replies

Tab Delimited Txt File To Php Html Table

Wouldn't you know that when I finally find a tutorial using Google that does exactly what I want, the code doesn't run when I use it ...

However, when it gets to this line, for ($i=0; $i<td>$line[0]</td>, it chokes on one of the '>'.

Since my php skills are very basic, and in no way include arrays, can someone assist me with this?

If it's easier to do it differently, what I'd like to do is create a table in Excel/Access, export it to the server (to a tab-delimted text file, since I'll be using commas in my table cells), and have it display in an HTML table.

If I can't figure this out, I'm tempted to just export to HTML from one or the other program (not sure quite yet which I would use), so ...

View 1 Replies

Extracting Delimited Text From A File

I want to extract several lines from an html file that begin with pattern1 and end with pattern2 and ignore everything before pattern1 and after pattern2.

View 1 Replies

Convert Tab Delimited Text File To XML?

I'm wondering if there is an accepted method to convert a tab delimited text file to XML, so that it can be imported in Adobe Flex. I would prefer to use PHP to achieve this.


The tab delimited text file I am looking to convert is in the format

Refnumber Location Name Age
123 North America Steve 32

And I am looking to convert to

<refnumber> 123 </refnumber>
<location> North America </location>
<name> Steve </name>
<age> 32 </age>

View 3 Replies

Reading Tab Delimited File Into Database

I have a .tab file that I need to write to the database. I've read it in, but for some reason it's not reading correctly. I'm truncating the complete database and then reloading it with whatever is in the tab file. I keep getting an error when trying to run my current code which is: Code:

View 5 Replies

Match Input Value With Values In A Delimited File

I'm very very new to php. I need your help. I have a text file which is ~ delimited. Each line has a name and link. I want to check if the link is similar to my input and if it is then return the name.

View 4 Replies

Importing To DB From Pipe Delimited Text File

I am trying to make a PHP script that will import data from a pipe delimited text file in to a database. The problem I am having is that the fields have long descriptions in them and there are multiple line breaks for paragraphs. They contain HTML codes as well (<B>).

The multiple line breaks are causing it to import the data strange, putting data in the wrong fields and totally missing some data as well. Here is what the text file Code:

View 14 Replies

Stripping Quotes Out Of A Comma Delimited File?

I have a flat file that I'm trying to stick into a MySQL database. One
record per line, multiple fields per record, and many of them are null
fields which are just double quotes without a space between. It's probably
nothing really major for people who have done this before, but I'm a bit
stumped. The file is comma delimited. Every field is surrounded by double
quotes. I've done quite a bit of searching, on the php site and elsewhere,
but I can't seem to get it to strip the quotes out so I can explode the file
line by line to grab the fields.

View 7 Replies

Reading From Comma Or Tab Delimited Text File?

I need to read the data from a file that can be either comma or tab delimited. I now that there is a function getcsv but it accepts only one possible delimiter.

View 6 Replies

Export Tab Delimited Text File From MySQL?

I'm trying to export a single row from a MySQL table to a text file and save it on the server using PHP...

Here's the code I'm trying to use in the exporttxt.php file:

// Define MySQL query
$sQuery = "SELECT * FROM tiwVehicles INTO OUTFILE '/var/www/html/myText.txt'";
// Run DB query

Nothing appears to happen. When I try to run the MySQL query in phpMyAdmin, I get the following error:

Code: #1045 - Access denied for user 'theimportwar'@'localhost' (using password: YES) From what I have read, it should be a relatively simple process to write a tab delimited TXT file to the web server using php.

View 1 Replies

Converting HTML Table To TXT Tab Delimited File?

I want to convert HTML table into TXT file. I dont know how to do it.

I also tried like creating text file from Browser. But its not working. But if i run the php program from putty, i creates a TXT file.

View 3 Replies

Tsv - Validate The Upload File Contains Tab Delimited Data?

I want to validate that an uploaded file is separated with tab delimiter. My file is not .CSV. Does anyone knows how to do this in PHP?

View 1 Replies

Copyrights 2005-15, All rights reserved