SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard
    Join Date
    Nov 2003
    Location
    United Kingdom
    Posts
    2,120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do I use php to convert a large pipe delimited file to a tab delimited file?

    Hi,

    I am working on a project and I have now come into a few problems where I am finding it hard to produce a working php script so that I can convert a Pipe delimited file to a Tab delimited file.

    So far I have the following, which doesn't work at all:

    PHP Code:
    <?
    $filename 
    'testing1.csv';
    $contents fopen($filename'r');
    $lines explode("\n"$contents);
    foreach(
    $lines as $line)
    {
    $cols[] = explode("|"$line);
    }
    echo 
    '<pre>';
    print_r($cols);
    echo 
    '</pre>';
    ?>
    Could you please take a look at it.

    Thanks!

  2. #2
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about:
    Code:
    $tmp = $filename . '.tmp';
    exec("mv $filename $tmp ; tr '\t' '|' < $tmp > $filename ; rm $tmp");
    Or:
    Code:
    exec("vi '+:.,\$s/[\t]/|/g'  $filename");
    Christopher

  3. #3
    SitePoint Wizard
    Join Date
    Nov 2003
    Location
    United Kingdom
    Posts
    2,120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK!

    I have found a way of doing it, but it is not working quite as I would like it to.

    What I have is the following:

    PHP Code:
    <?php
    $fp 
    fopen("testing1.csv""r") or die("Couldn't open file");
    $data fread($fpfilesize("$testing1.csv"));
    echo 
    "<textarea type=text name=cat1 rows=12 cols=44>$data</textarea>";
    $data str_replace("\t",""$data);
    $data str_replace("|","\t"$data);
    $data str_replace("\'","&amp;#39;"$data);
    $data str_replace('\"'"&amp;quot;"$data);
    fclose($fp);
    $fp fopen("testing2.csv""w") or die("Couldn't create new file");
    $numBytes fwrite($fp"$data");
    echo 
    "<textarea type=text name=catw1 rows=12 cols=44>$data</textarea>";
    fclose($fp);
    ?>
    As you can see, I am reading information from an csv file where I am then the str-replace tags to convert the file and then I am writing the converted data into a another file.

    So far every thing works apart from I am getting two when I open the csv file all I get is little squares where the pipe use to be and where words use to have one " they now have 2 of them. For example

    A word like: "how" now becomes: ""how""

    Could someone please take a look at my script and see what could be causing this.

    Thanks!

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2003
    Location
    United Kingdom
    Posts
    2,120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Could someone please, please take a look at my above script as I have now been trying to sort out the error for a while now and I still can't see why this is being caused.

    When there is data in my .CSV file that has a " in it, it then shows up on my page as "" instead of just the one.

    Can someone please take a look.

    Thanks!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •