dongxian5735 2015-03-11 18:11 采纳率: 0%
浏览 51
已采纳

如何从php数组返回重复值,以便为SEO创建短语?

I have a text content that is placed inside $content_array. The result must be one array containing words that are repeated, separated with commas. Now, how do I take out all the words that are repeating in that text, and put them in another array. My code so far (won't work):

$content = strip_tags($content);
// removing all html and php tags from content

$content = strtolower($content);
// transform all characters to lowercase

$content_array = str_word_count($content, 1);   
// converting content to array, in order to easier search the content, every member of array is one word 

$duplicates = array_unique(array_diff_assoc($content_array, array_unique($content_array))); 
// checking for all the words that are repeating and storing them into $duplicates array

    while ($val = current($duplicates)){            
    // going through an array to check for repeated values

        if($val >= '3'){                                
        // searching for repeated words, the ones that show up X times and more than X times (X = 3)

            $phrase = array($val);                  
            // storing value of repeated word into $phrase array

        }
        next($duplicates);                          
        // moving on to the next member in array

    }
$phrase = implode(",", $phrase);                
// separate phrases with comma
  • 写回答

1条回答 默认 最新

  • duangao8359 2015-03-11 18:46
    关注

    I think the problem is that you overwrite $phrase each time so you need to use $phrase[] = $val; to append to the array, but this is simpler:

    $content = strip_tags($content);
    $content = strtolower($content);
    $content_array = str_word_count($content, 1);   
    $phrase = array_filter(array_count_values($content_array), function($v) { 
                                                                   return ($v >= 3);
                                                               });
    

    Or instead of the array_filter:

    foreach($content_array as $key => $value) {
        if($value >= 3) {
            $phrase[] = $key;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?