简体   繁体   English

使用php通过单词或文本过滤csv文件

[英]Filter a csv file by a word or text using php

I have another csv file where I am trying to do a simple word filter. 我有另一个csv文件,我正在尝试做一个简单的单词过滤器。 For example, my text.csv file looks something like this: 例如,我的text.csv文件看起来像这样:

name, age, hobbies
Tom, 8, "football, soccer, baseball, wii, star wars, books"
Bill, 9, "football, baseball, ice hockey, basketball"
Sue, 8, "baseball, soccer, volleyball, bicycles, skating"
Mike, 8, "basketball, music, guitar, cartoons, books"
Ella, 9, "soccer, basketball, softball, clothes, books"
Tim, 9, "football, baseball, basketball, wii, cartoons"
Steven, 8, "baseball, soccer, star wars, cartoons, books"

I would like to filter by the third column. 我想按第三列过滤。 For example, if I filter by "wii" I will get rows 1 and 6 sent back: 例如,如果我按“ wii”进行过滤,我将得到发送的第1行和第6行:

Tom, 8, "football, soccer, baseball, wii, star wars, books"
Tim, 9, "football, baseball, basketball, wii, cartoons"

If I filter by "wii" or "guitar" I will get rows 1, 4, and 6 sent back. 如果我按“ wii”或“ guitar”过滤,我将得到第1、4和6行。

Tom, 8, "football, soccer, baseball, wii, star wars, books"
Mike, 8, "basketball, music, guitar, cartoons, books"
Tim, 9, "football, baseball, basketball, wii, cartoons"

I'm not good at php and arrays, but I've tried messing around with preg_match - but not sure if something like strpos is better. 我不太擅长php和数组,但是我尝试过弄乱preg_match-但不确定像strpos之类的东西是否更好。 Here's what I've done but can't get it to work right: 这是我所做的,但无法正常工作:

<?PHP
$firstWord = 'wii';
$secondWord = 'guitar';
$file = fopen('text.csv', 'r');
while (($line = fgetcsv($file)) !== FALSE) 
{  
list($name[], $age[], $hobbies[]) = $line;
if (preg_match($firstWord, $hobbies[0]) || (preg_match($secondWord,     $hobbies[0]) {
echo $name . ',"' .$age . '",' .$hobbies . "<br> \n";
} else {
    echo "A match was not found.<br> \n";
}}
?>

Any coding help is appreciated. 任何编码帮助表示赞赏。 It would also be nice to have the search be case-insensitive. 使搜索不区分大小写也很好。 Thanks for reading! 谢谢阅读!

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<br />\n";
    }
}

What about case insensitive searches. 不区分大小写的搜索呢? I'm curious to make this work amonst a list of my outlook contacts. 我很好奇让这项工作列入我的Outlook联系人清单。 Is there a better function to use than strtolower()?? 有没有比strtolower()更好的函数?

It's probably worth noting here that PHP has CSV handling libraries. 在这里可能值得注意的是PHP具有CSV处理库。

http://php.net/manual/en/function.fgetcsv.php http://php.net/manual/en/function.fgetcsv.php

This returns the next line of the CSV as an array eg 这会将CSV的下一行作为数组返回,例如

// csv file contains "col1,col2,col3"
// array = {'col1', 'col2', 'col3'}
$array = fgetcsv($csv);

Then you can simply use in_array() or a test on $array[column_number] to filter. 然后,您可以简单地使用in_array()或对$ array [column_number]进行测试以进行过滤。

I'm not sure if using a regular expression on the line string vs. using array search functions would be faster though, so it's probably worth testing. 我不确定在行字符串上使用正则表达式与使用数组搜索功能相比是否更快,因此可能值得测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM