如何计算长字符串中多个模式的出现次数?

时间:2009-10-02 20:47:14

标签: php regex

我有一个长字符串和一组国家/地区名称。所以数组看起来像这样:

array('Afghanistan', 'Bulgaria', 'United States', 'Bulgaria', ...)

我需要计算每个国家/地区出现在字符串中的次数。 是否有一种快速而又漂亮的方式来做到这一点,即某种神奇的preg_match_all接收一系列模式,或者我必须遍历所有国家?

5 个答案:

答案 0 :(得分:3)

我只是使用哈希表(关联数组)并遍历您的国家/地区:

// Count:
$country_names = array('Afghanistan', 'Bulgaria', 'United States', ...);
$country_count = array();
foreach ($country_names as $name) {
  $country_count[$name]++;
}

// Then display:
foreach ($country_names as $name) {
  echo "Found " . $country_count[$name] . " occurrences of $name.\n";
}

答案 1 :(得分:2)

如果您想要快速(但不能快速实施),请考虑Aho Corasick's algorithmHere是PHP中的一个实现。

答案 2 :(得分:2)

尝试使用substr_count http://us3.php.net/manual/en/function.substr-count.php

$yourtmplongstring = strtolower($yourlongstring);
# the above will solve any case sensitive issues
$country_names = array('Afghanistan', 'Bulgaria', 'United States', ...);
$country_count = array();
foreach ($country_names as $name) {
    $occurances = substr_count($name, $yourtmplongstring );
    $country_count[$name] = $occurances;
}

我希望这就是你要找的东西!

答案 3 :(得分:1)

您可以使用以下内容:

$country_names = array('Afghanistan', 'Bulgaria', 'United States', ...);
$country_names_preg = "/(" . implode("|", $country_names) . ")/";
preg_match_all($country_names_preg, $long_string, $matches);

//$matches will contain all of the country matches.
$echo "found: " . implode(", ", $matches);

// There would ideally be a check to make sure that $matches had something in it!

答案 4 :(得分:0)

我不认为你可以通过一次调用来做到这一点,但是为了这个目的,你在迭代substr_count()时可能比preg_ *更快。