All pastes #544198 Raw Edit

Someone

public text v1 · immutable
·published 2007-06-06 10:36 UTC
rendered paste body
<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php 

$headers = array(
   'GroupDesc' => 'v_categories_name_1',
   'LongDescription' => 'v_products_description_1',
   'Brand' =>  'v_manufacturers_name',
   'YourBuy' => 'v_products_price',
   'InStock' => 'v_products_quantity',
   'MelcoPart' => 'v_products_model'
);

// **** search phrase => replacement phrase 

$categories = array( 
   'FLOPPY DISK DRIVES' => 'Disk Drives', 
   'IDE DISK DRIVES' => 'Disk Drives',
   'STORAGE MISC' => 'Disk Drives',
   'MODEMS - GENERAL' => 'Communications',
   'GRAPHICS CARDS' => 'Graphics Cards',
   'PC CASES' => 'Cases',
   'MEMORY' => 'Memory',
   'GENERAL MEDIA' => 'Media',
   'CPUS' => 'CPUs',
   'PC ACCESSORIES' => 'Accessories',
   'PROJECTION & SCREEN' => 'Projectors',
   'MISC SOFTWARE' => 'Software',
   'SCSI DISK DRIVES' => 'Disk Drives',
   'PRINTER INKS' => 'Printer Consumables',
   'PRINTER PAPER' => 'Printer Consumables',
   'OPTICAL DRIVES' => 'DVD/CD',
   'MULTIMEDIA SPEAKERS' => 'Speakers',
   'SOUNDCARDS' => 'Soundcards',
   'PREMIER CAMERAS' => 'Digital Cameras',
   'KEYBOARDS' => 'Keyboards',
   'TV TUNERS' => 'TV Tuners',
   'MOTHERBOARDS' => 'Motherboards',
   'NETWORKING' => 'Networking',
   'CNET PRODUCTS' => 'Networking',
   'MONITORS' => 'Monitors',
   'MOTHERBOARD ACCESSOR' => 'Motherboards',
   'RACKMOUNT SERVERS' => 'Networking',
   'NOTEBOOKS' => 'Laptops'
); 
// open up source CSV file 
$csv = "bdt.csv";   // CSV file to be read and modified 
$fh = fopen($csv, 'r') or die("Could not open file"); 

// initialize data array 
$data = array(); 


// puts in the headers
// opens first line
$line = fgetcsv($fh,2000);
//counts headers in first line and tests them for contents of array $headers
for($i=0; $i < count($line); $i++) 
{
if(isset($headers[$line[$i]])) { $line[$i] = $headers[trim($line[$i])]; }
}
$line[] = 'v_status';
$data[] = $line;

// *** loop through each line in source file 
while(($line = fgetcsv($fh,2000,",",' " ')) !== FALSE) 
{   
   if(isset($line[2])) 
   { 
      // look for search values and replace them when found: 
      $line[2] = str_replace(array_keys($categories), $categories, $line[2]); 
   } 

   // ***** Add EOREOR to the end of everyline
   $line[] = 'EOREOR';
   $data[] = $line;  // add to data array 
} 

fclose($fh); 
// write data array to new CSV file: 
$newfile = 'data_fixed.csv'; 
$fh = fopen($newfile, 'w') or die("Could not open new file"); 
foreach($data as $line) 
{ 
   // use implode to create comma-separated line: 
   fwrite($fh, implode("\t", $line)."\n"); 
} 
fclose($fh); 
echo "<p>Revised data saved as $newfile.</p>"; 
?> 
 </body>
</html>