2017-02-28 10 views
0

私はPHPで文字列の中で最も使用されている単語を見つけようとしています。歌詞ファイルは歌詞の文字列です。文字列内で最も使用されている単語を見つける方法PHP

  //display the most used word in the lyrics file 
     $wordCount = str_word_count($lyrics); 
     $wordCountArray = array(); 
     foreach($wordCount as $word){ 
      if(!array_key_exists($word, $wordCountArray)){ 
       $wordCountArray[$word] = 1; 
      }else{ 
       $wordCountArray[$word] += 1; 
      } 
     } 
     arsort($wordCountArray); 
     print_r($wordCountArray[0]); 

私はこのコードでエラーが発生していると私は働いていないかと思っています。私は実際の言葉ではなく数字を必要とします。

+0

あなたの質問は何ですか? – Marcel

+0

これは '$歌詞'が何であるかによって変わるかもしれません。例を挙げる必要があるかもしれません。 –

+0

http://php.net/manual/en/function.substr-count.phpが役に立ちます – meen

答えて

1

私はあなたが意味を考える:

$words = str_word_count($lyrics, 1) 
foreach($words as $word) { 
0

簡単な例

<?php 
    $string = 'Hello hi hello abcd abc hoi bye hello'; 

    $string = strtolower($string); 
    $words = explode(' ',$string); 
    var_dump(array_count_values($words)); 
関連する問題