使用 PHP 查找重复的单词列表


find repeated list of words with php

这是一个简单的问题,我仍然不知道如何弄清楚。

我有一个单词列表,例如:

word
word
    one
    two
    three
    four
    five
    .
    ....X100
word
word

单词(一、二、三、四、五)完全重复,并且顺序相同,假设 100 次。 单词(单词)不重复,应忽略。

我想以完全相同的顺序获得(一、二、三、四、五)的列表。如何在 php 中做到这一点。到目前为止,我尝试的所有内容都只能计算单词的出现次数,并且不尊重单词顺序。

更新

谢谢大家的回答!

这是我的代码,仍然有很多错误

<?php
$str = 'word
word
word2
word3
one
two
three
four
one
two
three
four
one
two
three
four
one
two
three
four
one
two
three
four
one
two
three
four
yes
no 
do';
function repeated($str)
{
    $str=trim($str);  
    $str=ereg_replace('[[:space:]]+', ' ',$str);  
    $words=explode(' ',$str);  
    $lastWord = '';
    foreach($words as $w)  
    {  
        $wordstats[($w)]++;  
        if($lastWord!=''){
            $wordstats[$lastWord.' '.$w]++;
        }
        $lastWord = $w;
    }  
    foreach($wordstats as $k=>$v)  
    {  
        if($v>=2)  
        {  
            print "$k"." , ";  
        }  
    }  
}
print repeated($str);
?>

基本上我想要的是给 php $str文本,其中单词(一、二、三、四)在其中重复多次,并确定模式(一、二、三、四)仅此而已

现在,请考虑将其视为提示:

您可以使用计数变量和数组。创建所需单词的数组:

$SearchMe = array("One", "Two", "Three", "Four");
$Index = 0;

接下来,循环访问所需的列表。但不要检查"一"、"二"、"三",而是检查:

$SearchMe[$Index];

每次,你找到$SearchMe[$Index],迭代一个,直到你到达数组的末尾。一旦你到达那里,你就会增加另一个计数器,因为你已经找到了你的序列。在序列结束时,或者如果发现不匹配,请记住将$Index重置为 0。

这应该行得通。

更新我认为这不是你要找的答案。再次阅读您的问题后,我不太确定您实际上在问什么。因此,请向我们提供更多信息,以便我们更准确。

--

我想你正在寻找这样的东西。您的列表是数组吗?㞖:

    <?php      
    foreach ($list as $key => $value ){
      if (isarray($value)){
        $ListINeed[] = $value;
      }
    }
    ?>

当您在开篇帖子中发布数组时,这将返回:

    $ListINeed[1] = array(
      [0] => one
      [1] => two
      [2] => three
      [3] => four
      [4] => five
      [x] => .
      [100] => ....X100
    )

如果您确定 ListINeed 只存在一次,请删除 if 语句中第一段代码中$ListINeed后面的两个方括号 ([])。然后你会得到

    $ListINeed = array(
      [0] => one
      [1] => two
      [2] => three
      [3] => four
      [4] => five
      [x] => .
      [100] => ....X100
    )