Solved

How do i parse a text file with fields seperated by tabs?

Posted on 2004-10-12
8
238 Views
Last Modified: 2008-02-07
Hi.. I've got a tab-delimited text file that came exported out of a mysql database.

I want to feed these fields into another database but I need a way to input the seperate fields into different fields.

Only way i can come up with is using perl or cgi to split the line into array variables, using the tabs as seperators.

However, I'm at somewhat of a loss as to how to do this.
(I know a little more perl then i do php sadly)

Any ideas on how to do this in php please?

I've used stuff like:

$var=time();
$entrydate=date('d/m/Y',$var);

$explode = explode ('/', $entrydate);
$daydate=$explode[0];
$monthdate=$explode[1];
$yeardate=$explode[2];

to split the line up before but I've got no idea what the ascii code for a tab is?

thanks

Daryn
0
Comment
Question by:daryn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
8 Comments
 
LVL 27

Accepted Solution

by:
Diablo84 earned 300 total points
ID: 12289181
For tabs you will be wanting to use \t in a similar way that \n is used for new lines

$var = explode("\t",$yourstring);
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12289268
If the data is seperated by new lines and tabs, eg:

value1    value2    value 3    value4
value1    value2    value 3    value4
value1    value2    value 3    value4

you might first want to explode the file contents by \n to get each line into an array item

and then explode each of these items by \t to get the induvidual values

example:

$var = file_get_contents($_SERVER['DOCUMENT_ROOT']."/path/to/your/file.txt");

$phase1 = explode("\n",$var);

foreach ($phase1 as $var) {
 $phase2[] = explode("\t",$var);
}

You then have a 2 dimensional array assigned to $phase2:

you can view its contents using:

echo "<pre>";
print_r($phase2);
echo "</pre>";

or induvidual items can be accessed like this, if for example the first line in the file was
value1    value2    value 3    value4

echo $phase2[0][0]; //would output value1

0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12289382
To extend on that, you could also work the database inserting like this:

//DATABASE CONNECTION STRING

$var = file_get_contents($_SERVER['DOCUMENT_ROOT']."/path/to/your/file.txt");

$phase1 = explode("\n",$var);

foreach ($phase1 as $var) {
 $phase2 = explode("\t",$var);
 for ($i=0;$i<count($phase2);$i++) $phase2[$i] = "'".$phase2[$i]."'";
 $phase2 = implode(",",$phase2);
 $query = mysql_query("INSERT INTO tablename (field1,field2,field3,etc) VALUES ($phase2)") or die(mysql_error());
}

Untested. For a more specific example i would need to see an exact example of the files format but hopefully theres enough information here so you can work it out.


0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Expert Comment

by:Gyftus
ID: 12290104
The same exact solution as the one above, the only difference is that I have already tested it, you can change your fields accordingly, for the insertion in the database of you choice

            $myFile =  file_get_contents(//your file here//);
            $myFile = split("\r\n",$myFile);
            $myFileSize = sizeof($myFile);

            $upLoadDetails;

            
            for($i=0; $i<$myFileSize-1; $i++){
                  
                  
                  $tempD = split("\t",$myFile[$i]);
                  
                  $date = split("/",$tempD[0]);
                  $myDetails['date']            = mktime(0,0,0,$date[1],$date[0],$date[2]);
                  $myDetails['ax']            = addslashes($tempD[1]);
                  $myDetails['para']            = addslashes($tempD[2]);
                  $myDetails['client']      = addslashes($tempD[3]);
                  $myDetails['code']            = addslashes($tempD[4]);
                  $myDetails['item']            = addslashes($tempD[5]);
                  $myDetails['cx1']            = addslashes($tempD[6]);
                  $myDetails['cx2']            = addslashes($tempD[7]);
                  $myDetails['sup']            = addslashes($tempD[8]);
                  $myDetails['amount']      = addslashes($tempD[9]);
                  $myDetails['stock']            = addslashes($tempD[10]);

                  

            
                  

                  $query = "INSERT INTO info VALUES('{$i}',
                                                                        '{$myDetails['date']}',
                                                                        '{$myDetails['ax']}',
                                                                        '{$myDetails['para']}',
                                                                        '{$myDetails['client']}',
                                                                        '{$myDetails['code']}',
                                                                        '{$myDetails['item']}',
                                                                        '{$myDetails['cx1']}',
                                                                        '{$myDetails['cx2']}',
                                                                        '{$myDetails['sup']}',
                                                                        '{$myDetails['amount']}',
                                                                        '{$myDetails['stock']}')";
                  
                  mysql_query($query,$link) or die("Error :".mysql_error($link));

            }

Also if you know for a fact that your file lines ARE separated by \n only you can use the file() command which reads the file into an array, each position representing a line

Cheers
0
 

Expert Comment

by:Gyftus
ID: 12290140
Sorry the the  $upLoadDetails does nothing in this case, oops copy and paste mistake
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12290195
Gyftus, please do not reiterate examples that have already been given, if you want to expand on comments or provide alternative solutions then that is one thing, but posting a solution based upon the exact same methods as already explained by another user is not necessary. Thank you.
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question