Reputation: 1190
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?
Upvotes: 1
Views: 306
Reputation: 44992
Try using substr_count https://www.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!
Upvotes: 2
Reputation: 7594
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!
Upvotes: 1
Reputation: 778
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.
Upvotes: 0
Reputation: 91299
If you want something blazingly fast (but not quick to implement), consider Aho Corasick's algorithm. Here's an implementation in PHP.
Upvotes: 2
Reputation: 31280
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";
}
Upvotes: 3