$string = "apple grapes apple banana otherwords more words apple grapes apple orange orange orange orange orange orange orange";
是否有任何PHP函数会返回哪个字符串(香蕉,苹果,葡萄)在给定字符串中出现次数最多?在此示例中,它应返回apple
,但橙色的数字最多。
答案 0 :(得分:6)
参见代码+解释。
<?php
$interesting = array('banana', 'apple', 'grapes');
$string = "apple grapes apple banana otherwords more words apple grapes apple orange";
// Explode into array
$array = explode(" ", $string);
// Group the values
$count = array_count_values($array);
// Sort the grouping by highest occurence to lowest
arsort($count);
// Get the keys of the most occurring
$keys = array_keys($count);
// compare key against the $interesting array for what you're interested in
$most_occurring = '';
foreach ($keys as $i) {
if (in_array($i, $interesting, true)) {
$most_occurring = $i;
break;
}
}
// Print output
echo "Most occurring $most_occurring, $count[$most_occurring] occurences.";
?>
答案 1 :(得分:1)
虽然 rurouni88 的回答非常好,但它不再满足增加的要求,即并非所有单词都很重要,而是指定要查找的单词。你可以做到
<?php
$string = "apple grapes apple banana otherwords more words apple grapes apple orange orange orange orange orange orange orange";
$words=array("banana", "apple", "grapes");
foreach($words as $word)
{
$counts[$word]=substr_count($string,$word);
}
arsort($counts);
reset($counts);
if($counts[0]>0)
echo key($counts); // apple
else
echo "Your words are not present";
?>
<强> Fiddle 强>
答案 2 :(得分:0)
你可以试试这个:
$string = "apple grapes apple banana otherwords more words apple grapes apple orange orange orange orange orange orange orange";
$array = explode(" ", $string);
$words = array_unique($array);
$counter = 0;
$returnWord = null;
foreach($words as $word){
$currentWordCount = substr_count($string, $word);
if($currentWordCount > $counter){
$counter = $currentWordCount;
$returnWord = $word;
}
}
echo "Most occurrences: ".$returnWord ." : ".$counter;
Most occurrences: orange : 7
编辑:
$string = "apple grapes apple banana otherwords more words apple grapes apple orange orange orange orange orange orange orange";
$words = array("banana", "apple", "grapes");
$counter = 0;
$returnWord = null;
foreach($words as $word){
$currentWordCount = substr_count($string, $word);
if($currentWordCount > $counter){
$counter = $currentWordCount;
$returnWord = $word;
}
}
echo "Most occurrences: ".$returnWord ." : ".$counter;
Most occurrences: apple : 4
答案 3 :(得分:0)
是的,它被称为substr_count!
它接受字符串,事件的名称和要分析的子字符串。
例如,如果要检查该字符串中出现的最大次数,可以构建一个包含要检查的所有名称的数组,然后为每个值调用此函数。
示例源代码:
// initialize your string
$string = "apple grapes apple banana otherwords more words apple grapes apple orange";
// make an array with all the string names
$occurrences = explode(" ", $string);
// remove duplicate entries from the array
$occurrences = array_unique($occurrences);
// for every name, in an associative position, count the number of that occurrence in our string
foreach ($occurrences as $occurrence) {
$count[$occurrence] = substr_count($string, $occurrence);
}
// let see what it contains
var_dump($count);
// print out where it finds the max number of occorrences
echo "max occurrence found here: " . array_search(max($count), $count);
答案 4 :(得分:0)
rurouni88的解决方案既优雅又优雅,但由于分类,它的开销很小。对于大数据,最好的方法是在O(nm)中编写ad-hoc函数:
function max_occurrences($array, $valid) {
$count = array();
$maxValue = 0;
$maxEl = "";
foreach( $array as $v ){
if ( ! in_array($v, $valid, true) )
continue;
isset ( $count[$v] ) ? $count[$v]++ : $count[$v]=1 ;
if ( $count[$v] > $maxValue ) {
$maxValue = $count[$v];
$maxEl = $v;
}
}
return $maxEl;
}
$string = "apple grapes apple banana otherwords more words apple grapes apple orange orange orange orange orange orange orange";
echo max_occurrences(explode(" ",$string), array("apple","grapes", "banana"));