Jump to content

"debugging" a tab-delimited text file.


lifeson2112

Recommended Posts

ok, I have a slight problem. I need to read in information from a bunch of .xls spreadsheets into my database by first converting them into tab-delimited text files, but one of the columns is full of sentences with new lines.

 

I cut that column out, put it in a separate .xls file, and made a .txt file out of it.

 

Here's some info about how my program (gnumeric) changed it:

Each column (1 column in this case) is separated by a tab (checked the file for tabs using str_replace() and there aren't any).

and there is a carriage return at the end of each row. I want to keep the carriage returns in tact so I can know how to explode the file.

 

Here's some of my code so you can see how I have attempted to do this:

<?php
$file = file("power_wheels_caption.txt");
$rows = count($file);


for($i=0; $i<$rows; $i++)
{
    $noendlines[$i] = str_replace("\n"," ", $file[$i]);
}

$implodedstring = implode(" ",$noendlines);

$captionarray = explode("\r",$implodedstring);

$newfile = fopen("temp_caption.txt", 'w') or die("could not open file");

$newrow = count($captionarray);

for($j=0; $j<$newrow; $j++)
{
    fwrite($newfile, $captionarray[$j]."\r") or die("could not open file");
}
?>

 

I am planning then to take the "temp_caption.txt", read it into gnumeric again and paste the resulting cells back into the table, where it can be whole again. This will save a lot of time if someone can help me figure this out. Thanks in advance :).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.