douqian6315 2011-03-24 14:32
浏览 61
已采纳

将数组的值与字符串PHP匹配

I am working on a small project and I need some help. I have a CSV file with 150,000 rows (each row has 10 cols of data). I am using the fscvread to read the file and during the loop I want to match one of the columns (call it stringx) of each row against an array of 10,000 words. If any of the 10,000 words exist in stringx, it is removed using preg_replace.

Now all of this is fine. I have everything working fine, but the problem is, its too slow.

I have tried 2 methods to match the array. 1) I convert stringx to an array using explode(" ", $stringx) and then use the array_diff($array_stringx, $array_10000); 2) use foreach on $array_10000 and preg_replace on $stringx

Method 1 takes about 60 secs to go through 200 rows of data and method 2 can loop 500 rows in 60 secs.

Is there a better way to do this?

Once again, I am looking for an efficient way to (basically) array_diff an array of 10,000 words against 150,000 strings one at a time.

Help is much appreciated.

  • 写回答

6条回答 默认 最新

  • douwen1549 2011-03-24 15:16
    关注

    The following is just an alternative. It may or may not fulfil your requirements.

    It performs 84 ops/second with 10k words dictionary and 15k string on my laptop.

    Downside is that it does not remove the spaces around the words.

    $wordlist is just rows with one word each, could be a file.

    $dict = array_flip(preg_split('/
    /',$wordlist));
    
    function filter($str,$dict) {
      $words = preg_split('/\s/',$str);
      sort($words);
      $words = array_unique($words);
    
      foreach ($words as $word) {
        if (key_exists($word,$dict)) {
            $removeWords[] = '/\b' . $word . '\b/';
        }
      }
      return preg_replace($removeWords, '', $str);
    }
    

    Another example that performs a bit faster (107ops/s with 15kb string and 10k words dictionary)

    function filter2($str,$dict) {
      $words = preg_split('/\b/',$str);
      foreach ($words as $k => $word) {
        if (key_exists($word,$dict)) {
            unset($words[$k]);
        }
      }
      return implode('', $words);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题
  • ¥15 python点云生成mesh精度不够怎么办
  • ¥15 QT C++ 鼠标键盘通信
  • ¥15 改进Yolov8时添加的注意力模块在task.py里检测不到
  • ¥50 高维数据处理方法求指导
  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错