title: Filter a CSV file using php
body: <p>I've got a csv file being delivered via ftp twice a day.  I'm trying to write a php file to open this file, filter it by the category column, delete rows which don't have a certain term/terms in then save the file.</p>
  
  <p>I've tried lots of variants of snippets of code found on the internet but can't for the life of me figure out how to get it to work.  </p>
  
  <p>I tried using a variant of the code found in this question but couldn't make it work in my scenario</p>
  
  <p><a href="https://stackoverflow.com/questions/2382678/filter-a-csv-file-by-a-word-or-text-using-php">Filter a csv file by a word or text using php</a></p>
  
  <p>If it helps the csv file I am trying to edit is located here:
  <a href="http://accurateav.co.uk/sahara/saharafeed.csv" rel="nofollow noreferrer">http://accurateav.co.uk/sahara/saharafeed.csv</a> </p>
  
  <pre><code>&lt;?php
  $file  = fopen('saharafeed.csv', 'r');
  
  // You can use an array to store your search words, makes things more flexible.
  // Supports any number of search words.
  $words = array('casio');    
  // Make the search words safe to use in regex (escapes special characters)
  $words = array_map('preg_quote', $words);
  $regex = '/'.implode('|', $words).'/i';
  
  while (($line = fgetcsv($file)) !== FALSE) {  
      list($ExportDate, $ItemCode, $Manufacturer, $Model, $ProductName, $DealerPrice, $Stock, $RRP, $Category, $ShortDesc, $LongDesc, $Weightkg, $EAN, $NonStock, $LargePictureURL, $SpecSheet, $HiResPhoto1, $HiResPhoto2) = $line;
  
      if(preg_match($regex, $Category)) {
          echo "$ExportDate, $ItemCode, $Manufacturer, $Model, $ProductName, $DealerPrice, $Stock, $RRP, $Category, $ShortDesc, $LongDesc, $Weightkg, $EAN, $NonStock, $LargePictureURL, $SpecSheet, $HiResPhoto1, $HiResPhoto2&lt;br /&gt;\n";
      }
  }
  
  ?&gt;
  </code></pre>
  
  <p>This is the code I have used so far to filter the category to only show products in the Casio category however when i run the script no results are shown even though there are numerous products under the casio category.</p>