简体   繁体   English

用php查找单词的重复列表

[英]find repeated list of words with php

This is an easy question, still I don't how how to figure it out. 这是一个简单的问题,但我仍然不知道如何解决。

I have a list of words like: 我有一个类似的单词列表:

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

with the words (one, two, three, four, five) repeated exactly, and in the same order, let's say 100 times. 单词(1、2、3、4、5)完全重复,并且以相同的顺序重复100次。 the words (word) are not repeated and should be ignored. 单词(word)不会重复,应该忽略。

I want to get that list of (one, two, three , four, five) with exactly the same order. 我想以完全相同的顺序获取该列表(一,二,三,四,五)。 How can this be done in php. 如何做到这一点在PHP中。 All I tried until now can just count number of occurrences of words, and don't respect the word order. 到目前为止,我所尝试的所有工作都只能计算单词出现的次数,并且不尊重单词顺序。

Update 更新资料

Thank you for you answers everyone! 谢谢大家的回答!

here is my code, still has a lot of errors 这是我的代码,仍然有很多错误

<?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);

?>

basically what I want is to give php that $str text, which will have the words (one,two,three,four) repeated many times inside of it, and determine the pattern (one,two,three,four) that's all 基本上我想要的是给php $ str文本,里面会有很多重复的单词(一,二,三,四),并确定了模式(一,二,三,四)

Now, please consider this as a hint: 现在,请将此作为提示:

You could use a counting variable and an Array. 您可以使用一个计数变量和一个数组。 Create an array of those words you want: 创建所需的这些单词的数组:

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

Next, iterate through that list you want. 接下来,遍历所需的列表。 But don't check for 'One', 'Two', 'Three', but rather for: 但是不要检查“一”,“二”,“三”,而要检查:

$SearchMe[$Index];

Each time, you FIND $SearchMe[$Index], iterate it by one, until you're at the end of the array. 每次,您查找$ SearchMe [$ Index],将其迭代一次,直到您位于数组的末尾。 Once you're there, you increment another counter, for you have found your sequence. 到达那里后,您将增加另一个计数器,因为您已找到序列。 At the end of the sequence, or if you find a mismatch, remember to reset $Index to 0. 在序列的末尾,或者如果发现不匹配,请记住将$ Index重置为0。

That should work. 那应该工作。

update I think this isn't the answer you are looking for. 更新我认为这不是您要找的答案。 After reading your question again I am not quite sure what you are actually asking. 再次阅读您的问题后,我不太确定您实际上在问什么。 So please provide us with more information so we can be more accurate. 因此,请向我们提供更多信息,以便我们更加准确。

-- -

I think you are looking for something like this. 我认为您正在寻找类似的东西。 Is your list an array? 您的列表是数组吗? If so: 如果是这样的话:

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

When you have the array you posted in your opening post, this will return: 当您将数组放置在开头的帖子中时,将返回:

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

If you are sure that the ListINeed will only be there once, delete the two brackets ([]) after the $ListINeed in the first piece of code in the if-statement. 如果您确定ListINeed仅会出现一次,请在if语句的第一段代码的$ ListINeed之后删除两个方括号([])。 Then you will get 然后你会得到

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM