【问题标题】:Filter a csv file by a word or text using php使用 php 按单词或文本过滤 csv 文件
【发布时间】:2011-01-23 20:54:26
【问题描述】:

我有另一个 csv 文件,我正在尝试做一个简单的单词过滤器。例如,我的 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"

我想按第三列过滤。例如,如果我按“wii”过滤,我将返回第 1 行和第 6 行:

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

如果我按“wii”或“吉他”进行过滤,我将返回第 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"

我不擅长 php 和数组,但我尝试过使用 preg_match - 但不确定 strpos 之类的东西是否更好。这是我已经完成但无法正常工作的内容:

<?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";
}}
?>

感谢任何编码帮助。让搜索不区分大小写也很好。感谢阅读!

【问题讨论】:

    标签: php text csv filter


    【解决方案1】:

    你已经接近了。这样的事情应该可以工作:

    $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";
        }
    }
    

    【讨论】:

    • 谢谢,我收到了一个“{”错误,但后来我更改了最后几行,在 preg_match 末尾添加了第二个“)”。效果很好,谢谢大图!如何使搜索不区分大小写,以便搜索“吉他”或“吉他”无关紧要?
    • @Thomas: $regex = '/'.implode('|', $words).'/i'; 使用不区分大小写的标志 i
    • 非常感谢杰森巴。你们都帮了我很大的忙。非常感谢。
    【解决方案2】:

    不区分大小写的搜索怎么样。我很想把这项工作放在我的 Outlook 联系人列表中。有没有比 strtolower() 更好的函数??

    【讨论】:

      【解决方案3】:

      这里可能值得注意的是,PHP 有 CSV 处理库。

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

      这会将 CSV 的下一行作为数组返回,例如

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

      然后你可以简单地使用 in_array() 或 $array[column_number] 上的测试来过滤。

      我不确定在行字符串上使用正则表达式是否会比使用数组搜索函数更快,所以它可能值得测试。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-04-10
        • 1970-01-01
        • 1970-01-01
        • 2014-12-10
        • 2011-11-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多