SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  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 show grab columns of data from a .txt file and then write it to another file

    Hi,

    I have a text file with information in, something like the following:

    PHP Code:
    ProductID|Name|MerchantID|Merchant|Link|Thumbnail|BigImage|Price|RetailPrice|Category|SubCategory|Description|Custom1|Custom2|Custom3|Custom4|Custom5|LastUpdated
    453368991
    |10&quotWoodworker&39;s Table Saw with 30&quotFence|3351|AcmeHardware.com|http://www.shareasale.com/m-pr.cfm?merchantID=3351&userID=71112&productID=453368991|http://images.acmehardware.com/200x200/4291225.jpg|http://images.acmehardware.com/200x200/4291225.jpg|1042.56||Home/Family|Home Improvement|Accurate, dependable fence delivers smooth action  Cast iron trunnion provides solid arbor support  Blade elevation crank with adequate hand clearance  Accurate blade angle adjustments  Large, easy-access on/off switch  Equipped with a Dust Collection Por||||Power Tools:Saws:Table Saws|DeWalt|2004-09-07 18:49:23.53 
    With the above information, I am trying to change it to tab delimited and I also only want a few of the rows. I would like the Name, Thumbnail, Price, Link and the Description columns. I would also like to merge the Category and SubCategory columns an also include them into the tab delimited file.

    I have been trying to do this for a while now an I have had no success.

    Could someone please help me.

    So far I have the following:

    <?php

    $fp = @fopen("person.txt", "r") or die("Couldn't open file");

    while(!feof($fp))
    {
    $data .= fgets($fp, 1000);
    }

    fclose($fp);

    $values = explode("|", $data);

    echo "Category: " . $values[0] . " " . $values[1] . "<br>";
    echo "Name: " . $values[2] . "<br>";
    echo "Thumbnail: " . $values[3] . "<br>";
    echo "Price: " . $values[4] . "<br>";
    echo "Link: " . $values[5] . "<br>";
    echo "Description: " . $values[7];
    ?>

    I hope that you can help as I have been trying to do this for a while now.

    Thanks!

  2. #2
    SitePoint Addict bkennedy's Avatar
    Join Date
    Aug 2004
    Location
    michigan
    Posts
    251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If each record is on its own line, use
    PHP Code:
    $count 0;
    $people file("person.txt");
    foreach (
    $people as $p)
    {
    $values explode ("|"$p);

    $cats[$count] = $values[0] . " " $values[1];
    $names[$count] =  $values[2];
    $thumnails[$count] = $values[3];
    $prices[$count] = $values[4];
    $links[$count] = $values[5];
    $descs $values[7];
    $count++;
    }
    //i think the tab character is "\t"
    $fp fopen("newfile.txt","w");
    for (
    $x=0;$x<count($cats);$x++)
    {
    fwrite($fp,$cats[$x]);
    fwrite($fp,"\t");
    fwrite($fp,$names[$x]);
    fwrite($fp,"\t");
    .  
    //for each of the values you want inthe new file
    .
    .
    fwrite($fp,"\n");   //newline

    ----------------
    bob.kennedy
    ----------------

  3. #3
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If the file is exactly as you originally described you could also do:
    PHP Code:
    <?php
    //the new headings
    $headings = array('Name','Category','Thumbnail','Price','Link','Description');
    $out[] = join("\t"$headings);
    //the array slice skips the first line of the file (the line with the headings)
    foreach(array_slice(file('test.txt'), 1) as $line)
    {
        
    $parts explode('|'$line);
        
    $new = array();
        
    $new[] = $parts[1];
        
    $new[] = $parts[9].' '.$parts[10];
        
    $new[] = $parts[5];
        
    $new[] = $parts[7];
        
    $new[] = $parts[4];
        
    $new[] = $parts[11];
        
    $out[] = join("\t"$new);
    }
    //echo the output to test
    echo join('<br />'$out);
    //write the new file
    $fp fopen('newfile.txt''w');
    fputs($fpjoin("\n"$out));
    fclose($fp);
    ?>

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

    Thanks for the replies bkennedy and markl999.

    I had tried you example script out first of all bkennedy where it write the results to my file. I then added more columns to be writed to the new file, but for some reason then, it was not writing the information to the new file for some reaon so I then tried markl999's example, which worked fine.

    I added more columns to it so they could be written to the new file and things went fine.

    The only problem that I have with it now is that the first line is still being displayed in the text file that is written. This first line is the headings, which I don't want to be included.

    This is the text file that I tried to delimit and write:

    PHP Code:
    ProductID|Name|MerchantID|Merchant|Link|Thumbnail|BigImage|Price|RetailPrice|Category|SubCategory|Description|Custom1|Custom2|Custom3|Custom4|Custom5|LastUpdated
    453368991
    |10&quotWoodworker&39;s Table Saw with 30&quotFence|3351|AcmeHardware.com|http://www.shareasale.com/m-pr.cfm?merchantID=3351&userID=71112&productID=453368991|http://images.acmehardware.com/200x200/4291225.jpg|http://images.acmehardware.com/200x200/4291225.jpg|1042.56||Home/Family|Home Improvement|Accurate, dependable fence delivers smooth action  Cast iron trunnion provides solid arbor support  Blade elevation crank with adequate hand clearance  Accurate blade angle adjustments  Large, easy-access on/off switch  Equipped with a Dust Collection Por||||Power Tools:Saws:Table Saws|DeWalt|2004-09-07 18:49:23.53
    453345406|Dlx Stainless Steel Gas Grill|3351|AcmeHardware.com|http://www.shareasale.com/m-pr.cfm?merchantID=3351&userID=71112&productID=453345406|http://images.acmehardware.com/200x200/3314044.jpg|http://images.acmehardware.com/200x200/3314044.jpg|964.93||Home/Family|Home Improvement|Features stainless steel side shelves Polished welds & spring handle H-D 16 ga stainless steel construction Reliable spark ignitor-24,000 BTU output Dual controls for temp range 550-300 degrees Warranty:10 years on Stainless Steel drip pan & lifetime- on ||||AcmeHardware.com:New Items:New Specials - Just Added|Phoenix Grill|2004-09-07 18:49:23.53
    453368840|5500 Watts Generator|3351|AcmeHardware.com|http://www.shareasale.com/m-pr.cfm?merchantID=3351&userID=71112&productID=453368840|||923.04||Home/Family|Home Improvement|||||AcmeHardware.com:New Items:New Specials - Just Added|Porter Cable|2004-09-07 18:49:23.53
    453342557|5.5 HP Gas 8 Gallon Portable Compressor|3351|AcmeHardware.com|http://www.shareasale.com/m-pr.cfm?merchantID=3351&userID=71112&productID=453342557|http://images.acmehardware.com/200x200/4203147.jpg|http://images.acmehardware.com/200x200/4203147.jpg|903.46||Home/Family|Home Improvement|5.5 HP Honda GX gas engine provides proven reliability and durability   Twin cylinder, cast iron, oil lubricated pump for durability and reliability   Ball drain valve allows for quick and thorough tank draining   Automatic idle control for reduced fuel c||||AcmeHardware.com:New Items:New Specials|DeWalt|2004-09-07 18:49:23.53
    453345405|Stainless Gas Grill|3351|AcmeHardware.com|http://www.shareasale.com/m-pr.cfm?merchantID=3351&userID=71112&productID=453345405|http://images.acmehardware.com/200x200/3314036.jpg|http://images.acmehardware.com/200x200/3314036.jpg|856.1||Home/Family|Home Improvement|Die cast aluminum end caps Full length perforated stainless steel handle H-D 16 ga construction-polished stainless steel 24,000 BTU output-Dual controls with- temperature range from 550 to 300 degrees Reliable spark ignitor Warranty:10 years on Stainless ||||AcmeHardware.com:New Items:New Specials - Just Added|Phoenix Grill|2004-09-07 18:49:23.53
    453329640|Tankless Water Heater|3351|AcmeHardware.com|http://www.shareasale.com/m-pr.cfm?merchantID=3351&userID=71112&productID=453329640|||738.94||Home/Family|Home Improvement|||||Plumbing:Water Heaters & Parts:Electric Water Heaters|Controlled Energy|2004-09-07 18:49:23.53
    453339218|16d Masonry Cut Nails|3351|AcmeHardware.com|http://www.shareasale.com/m-pr.cfm?merchantID=3351&userID=71112&productID=453339218|http://images.acmehardware.com/200x200/0102186.jpg|http://images.acmehardware.com/200x200/0102186.jpg|100.1||Home/Family|Home Improvement|Ideal for nailing furring strips to cinder blocks  Also for nailing to new concrete & brick walls  Blunt point & tapered shank  Less spalling during nailing  Hold tighter than round wire nails  Also used to face-nail hardwood flooring  3-1/2&quot;||||AcmeHardware.com:New Items:New Specials|Maze Nails|2004-09-07 18:49:23.53
    453332792|Wh Showercove Shwr Surrnd|3351|AcmeHardware.com|http://www.shareasale.com/m-pr.cfm?merchantID=3351&userID=71112&productID=453332792|http://images.acmehardware.com/200x200/5130703.jpg|http://images.acmehardware.com/200x200/5130703.jpg|100.1||Home/Family|Home Improvement|Five-panel shower surround Adjustable 32&quot; to 36&quot; Use with 32WL, 34WL, 36WL, 9950, 9951||||AcmeHardware.com:New Items:New Specials - Just Added|Crane|2004-09-07 18:49:23.53
    453326606|Quickwire Push-in Sidewire|3351|AcmeHardware.com|http://www.shareasale.com/m-pr.cfm?merchantID=3351&userID=71112&productID=453326606|http://images.acmehardware.com/200x200/5751425.jpg|http://images.acmehardware.com/200x200/5751425.jpg|100.08||Home/Family|Home Improvement|Rated: 15A - 125V Grounding  Contractors Pack  10 per pack  Ivory||||Electrical:Receptacles:Duplex Receptacles|Leviton|2004-09-07 18:49:23.53
    453326607|Quickwire Push-in Sidewire|3351|AcmeHardware.com|http://www.shareasale.com/m-pr.cfm?merchantID=3351&userID=71112&productID=453326607|http://images.acmehardware.com/200x200/5751433.jpg|http://images.acmehardware.com/200x200/5751433.jpg|100.08||Home/Family|Home Improvement|Rated: 15A - 125V Grounding  Contractors Pack  10 per pack  White||||Electrical:Receptacles:Duplex Receptacles|Leviton|2004-09-07 18:49:23.53
    453341386|Drywall Screwdriver (Variable SpeedReversible)|3351|AcmeHardware.com|http://www.shareasale.com/m-pr.cfm?merchantID=3351&userID=71112&productID=453341386|http://images.acmehardware.com/200x200/4260139.jpg|http://images.acmehardware.com/200x200/4260139.jpg|99.97||Home/Family|Home Improvement|6.5 Amp, 0-6,000 RPM  Silent Synchronized Pin Clutch  Retractable Belt Clip  Lightweight Cast Aluminum Gear Housing  Adjustable Depth Locator with Sure-Lock  Forward/Reverse Switch Near Trigger  Includes: (1)2 Phillips Insert Bit  Magnetic Bit Holder & ||||AcmeHardware.com:New Items:New Specials - Just Added|Makita|2004-09-07 18:49:23.53
    453351350|W.HMaze 3/4&quotmasonry nails|3351|AcmeHardware.com|http://www.shareasale.com/m-pr.cfm?merchantID=3351&userID=71112&productID=453351350|http://images.acmehardware.com/200x200/1112671.jpg|http://images.acmehardware.com/200x200/1112671.jpg|99.91||Home/Family|Home Improvement|3/4&quot; hardened steel masonry nails with9 gauge, fluted shank and a flat head. 50 lb. box.||||Hardware:Nails:Concrete & Masonry Nails|Maze Nails|2004-09-07 18:49:23.53
    453339303|3 Ia Fiber Step Ladder|3351|AcmeHardware.com|http://www.shareasale.com/m-pr.cfm?merchantID=3351&userID=71112&productID=453339303|http://images.acmehardware.com/200x200/6075865.jpg|http://images.acmehardware.com/200x200/6075865.jpg|99.88||Home/Family|Home Improvement|Tool-Tra-Top includes:  Dual drill holsters, tool & paint can holders  Small parts tray  Ideal for one or two person jobs  300 lb. capacity per side of ladder  Drill not included||||AcmeHardware.com:New Items:New Specials|Werner|2004-09-07 18:49:23.53
    453336196|2+2 3.5 Raise Panel Bit|3351|AcmeHardware.com|http://www.shareasale.com/m-pr.cfm?merchantID=3351&userID=71112&productID=453336196|http://images.acmehardware.com/200x200/4495719.jpg|http://images.acmehardware.com/200x200/4495719.jpg|99.8||Home/Family|Home Improvement|Shims included to adjust tongue thickness  54-562 radius backcutter included  Cuts all composition materials, plywood  Hard wood and soft wood  Use on table mounted portable routers  1/2&quot; shank diameter  Size: overall diameter x carbide height||||AcmeHardware.com:New Items:New Specials|Freud|2004-09-07 18:49:23.53
    453325407|26 1-1/8 Cl108 Pine Bifold|3351|AcmeHardware.com|http://www.shareasale.com/m-pr.cfm?merchantID=3351&userID=71112&productID=453325407|http://images.acmehardware.com/200x200/7775299.jpg|http://images.acmehardware.com/200x200/7775299.jpg|99.72||Home/Family|Home Improvement|Three raised panels  Clear stiles  1 Finger joint stiles  Thickness: 1-1/8&quot;  Height: 81&quot;  Hardware included||||TBD Categories:Categories:TBD|Ledco|2004-09-07 18:49:23.53
    453350228|White Aluminum Round Column|3351|AcmeHardware.com|http://www.shareasale.com/m-pr.cfm?merchantID=3351&userID=71112&productID=453350228|http://images.acmehardware.com/200x200/1919018.jpg|http://images.acmehardware.com/200x200/1919018.jpg|99.66||Home/Family|Home Improvement|For residential & commercial use  Also for construction & renovation  Maintenance free, easy to assemble  Includes cap & base  Column height: 8&39;  Color: white  Type: round  White||||TBD Categories:Categories:TBD|Afco Industries|2004-09-07 18:49:23.53
    453342825|Open Slide Ii Shelf|3351|AcmeHardware.com|http://www.shareasale.com/m-pr.cfm?merchantID=3351&userID=71112&productID=453342825|http://images.acmehardware.com/200x200/4190906.jpg|http://images.acmehardware.com/200x200/4190906.jpg|99.54||Home/Family|Home Improvement|Durabe and strong  Open slide rod for more storage  Color: white  12&quot; depth||||AcmeHardware.com:New Items:New Specials|Schulte|2004-09-07 18:49:23.53
    453332072|Moen single handle tub and shower combination fitting|3351|AcmeHardware.com|http://www.shareasale.com/m-pr.cfm?merchantID=3351&userID=71112&productID=453332072|http://images.acmehardware.com/200x200/5142070.jpg|http://images.acmehardware.com/200x200/5142070.jpg|99.42||Home/Family|Home Improvement|Washerless single handle tub and shower combination fitting with contemporary lever. Pressure balance prevents scalding surprises. Includes diverter spout, showerhead, arm, and flange. Limited lifetime warranty||||Kitchen & Bath:Tub & Shower Fittings:Tub/Shower Valve Kits|Moen|2004-09-07 18:49:23.53
    453351945|5g Concrete F&d Sealer|3351|AcmeHardware.com|http://www.shareasale.com/m-pr.cfm?merchantID=3351&userID=71112&productID=453351945|||99.16||Home/Family|Home Improvement|Clear gloss sealer for bare concrete Lower VOC content- than high performing urethanes Covers 200 - 300 sq. ft. per gallon Produces a hard, clear film when dry Non-yellowing, good gloss retention||||AcmeHardware.com:New Items:New Paint|Convenience Products|2004-09-07 18:49:23.53 
    And this is the script that I have so far:

    PHP Code:
    //the new headings
    $headings = array('Merchant','Link','Name','Description','Thumbnail','Category','Price');
    $out[] = join("\t"$headings);
    //the array slice skips the first line of the file (the line with the headings)
    foreach(array_slice(file('acmedone.txt'), 1) as $line)
    {
        
    $parts explode('|'$line);
        
    $new = array();
        
    $new[] = $parts[3];
        
    $new[] = $parts[4];
        
    $new[] = $parts[1];
        
    $new[] = $parts[11];
        
    $new[] = $parts[5];
        
    $new[] = $parts[9].' > '.$parts[10];
        
    $new[] = $parts[7];
        
    $out[] = join("\t"$new);
    }
    //echo the output to test
    echo join('<br />'$out);
    //write the new file
    $fp fopen('C:\phpdev\www\produpload\convert/acmehardware1.txt''w');
    fputs($fpjoin("\n"$out));
    fclose($fp); 
    I hope you can help me with this!

    Thanks!

  5. #5
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    //the new headings
    $headings = array('Merchant','Link','Name','Description','Thumbnail','Category','Price');
    $out[] = join("\t"$headings); 
    Just remove those lines.

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

    I have been looking to produce a script like this for a while. As I couldn't quite manage to do it I then resorted to Excel, which I couldn't automate 100%. So I then thought that there must be a way using php and now I have done it because of you.

    Thanks!

    Also, I didn't look at your comment near the top by the array, which is the reason why I missed that part.

    But everything works just fine now.

    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
  •