1. $f = file('words.txt');
  2. $words = array();
  3. foreach ($f as $w) {
  4. $words[] = preg_quote(trim($w), '/');
  5. }
  6. $text = file_get_contents('text.txt');
  7. $start = microtime(true);
  8. $reg = '/' . implode('|', $words) . '/S';
  9. preg_match_all($reg, $text, $m);
  10. $result = array();
  11. $total = 0;
  12. foreach ($m[0] as $w) {
  13. if (!isset($result[$w])) {
  14. $result[$w] = 1;
  15. } else {
  16. $result[$w]++;
  17. }
  18. $total++;
  19. }
  20. $end = microtime(true);
  21. echo $end - $start, "\n";
  22. echo $total, "\n";
  23. print_r($result);
复制代码

PHP


09-14 21:13