I have another csv file where I am trying to do a simple word filter. For example, my text.csv file looks something like this:
name, age, hobbies
Tom, 8, \"
You're close. Something like this should work:
$file = fopen('text.csv', 'r');
// You can use an array to store your search words, makes things more flexible.
// Supports any number of search words.
$words = array('wii', 'guitar');
// Make the search words safe to use in regex (escapes special characters)
$words = array_map('preg_quote', $words);
// The argument becomes '/wii|guitar/i', which means 'wii or guitar, case-insensitive'
$regex = '/'.implode('|', $words).'/i';
while (($line = fgetcsv($file)) !== FALSE) {
list($name, $age, $hobbies) = $line;
if(preg_match($regex, $hobbies)) {
echo "$name, $age, $hobbies
\n";
}
}