tags:

views:

102

answers:

5

I have a long string, and an array of country names. So the array looks something like this:

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

I need to count the number of times each country appears in the string. Is there a quick and nifty way of doing this, i.e., some kind of magical preg_match_all which receives an array of patterns, or must I iterate through all countries?

A: 

I don't think you can do it with one call, but while you're iterating through substr_count() may be faster than preg_* for this purpose.

Ryan
+3  A: 

I'd just use a hash table (associative array) and loop through your countries:

// 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";
}
pix0r
I'm not sure that is what he ia after... I think he has a string with plenty of other text it in and wants to count the occurance of each country in that string.
Lizard
yet it is brilliant
Reinis I.
+1  A: 

If you want something blazingly fast (but not quick to implement), consider Aho Corasick's algorithm. Here's an implementation in PHP.

JG
+1  A: 

Try using 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;
}

I hope this is what you were looking for!

Lizard
Thanks, that would work - but it's case sensitive. So if I have BULGARIA in the text, It's not going to work.
bosh
do a strtolower() before.
Toto
Please check my edit as this would solve your case sensitive issues.
Lizard
+1  A: 

You can use something like:

$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!
null