dongxian5735 2015-03-11 18:11
浏览 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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)