Php 删除包含特定字数的行

Php 删除包含特定字数的行,php,Php,我的问题可以通过下面的例子来理解: 假设这是文本文件,其中包含以下行: 您好,这是我的word文件,这是第1行 您好,这是第二行,这是一些文字 您好,这是第三行,还有一些文字 jhasg djgha Sdgasgdjasgh jdkh sdhgfkjg sdjhgf sjkdghf sdhf s hdg fjhsgd fjhgsdj gfj ksdgh 我想把每一行都变成一个变量 然后将该行的所有单词放入一个数组中 然后将包含该行单词的数组与下一行的所有单词进行比较 如果单词匹配数超过3,则删

我的问题可以通过下面的例子来理解:

假设这是文本文件,其中包含以下行:

您好,这是我的word文件,这是第1行
您好,这是第二行,这是一些文字
您好,这是第三行,还有一些文字
jhasg djgha Sdgasgdjasgh jdkh
sdhgfkjg sdjhgf sjkdghf sdhf
s hdg fjhsgd fjhgsdj gfj ksdgh

  • 我想把每一行都变成一个变量
  • 然后将该行的所有单词放入一个数组中
  • 然后将包含该行单词的数组与下一行的所有单词进行比较
  • 如果单词匹配数超过3,则删除该行
因此,在上述示例中,输出应为:

您好,这是我的word文件,这是第1行
jhasg djgha Sdgasgdjasgh jdkh
sdhgfkjg sdjhgf sjkdghf sdhf
s hdg fjhsgd fjhgsdj gfj ksdgh

因为
hello this is line
超过3个单词,所以包含这些单词的行被删除。请注意,第一行不会被删除,因为它是唯一的

我试着自己编写代码,创建了一个200 MB的文本文件,第一行文本数量不限。不管怎么说,这是代码,不要执行它,否则你的硬盘可能会被填满

<?php

$fileA = fopen("names.txt", "r");
$fileB = fopen("anothernames.txt", "r");
$fileC = fopen("uniquenames.txt", "w");
while(!feof($fileA))
{
    $line = fgets($fileA);
    $words = explode(" ", $line);
    $size = count($words);

    while(!feof($fileA))
    {
        $line1 = fgets($fileB);
        $words1 = explode(" ", $line1);
        $size1 = count($words1);

        $c=0;

        for($i=0; $i<$size; $i++)
        {
                for($j=0; $j<$size1; $j++)
            {
                    if($words[$i]==$words1[$j])
                        $c++;
            }
        }
        if($c<3)
            fwrite($fileC, $line);
    }
}

fclose($fileA);
fclose($fileB);
fclose($fileC);

?>

谢谢


if($c一个简单的方法是:

  • 使用
    file()
  • 创建一个数组,包含按每个单词索引的句子
  • 最后,为出现在任何数组中的每个句子建立一个黑名单,对任何单词计算超过3个条目
  • 然后打印除黑名单之外的每一行:
例如:

    <?php
$lines = array("hello this is my word file and this is line number 1",
  "hello this is second line and this is some text",
  "hello this is third line and again some text",
  "jhasg djgha sdgasjhgdjasgh jdkh",
  "sdhgfkjg sdjhgf sjkdghf sdhf",
  "s hdg fjhsgd fjhgsdj gfj ksdgh");

//$lines = file("path/to/file");

$result = array();
//build "count-per-word" array
foreach ($lines AS $line){
   $words = explode(" ", $line);
   foreach ($words AS $word){
       $word = strtolower($word);
       if (isset($result[$word]))
           $result[$word][] = $line;
       else
           $result[$word] = array($line);  
   }
}

//Blacklist each sentence, containing a word appearing in 3 sentences.
$blacklist = array();
foreach ($result AS $word => $entries){
   if (count($entries) >= 3){
     foreach($entries AS $entry){
       $blacklist[] = $entry;
     }
   }
}

//list all not blacklisted. 
foreach ($lines AS $line){
  if (!in_array($line, $blacklist))
      echo $line."<br />";
}

?>
请注意,这也会将包含3倍相同单词的单个句子列入黑名单,例如“Foo Foo Foo bar”

要进行此操作,请在将某个单词推送到数组之前检查该行是否已“已知”:

foreach ($words AS $word){
   if (isset($result[$word])){
       if (!in_array($line, $result[$word])){
          $result[$word][] = $line;
       }
   }else
       $result[$word] = array($line);  
}

为什么不干脆
数组_intersect

php > $l1 = 'hello this is my word file and this is line number 1';
php > $l2 = 'hello this is second line and this is some text';
php > $a1 = explode(" ", $l1);
php > $a2 = explode(" ", $l2);
php > var_dump(array_intersect($a1, $a2));
array(7) {
  [0]=>
  string(5) "hello"
  [1]=>
  string(4) "this"
  [2]=>
  string(2) "is"
  [6]=>
  string(3) "and"
  [7]=>
  string(4) "this"
  [8]=>
  string(2) "is"
  [9]=>
  string(4) "line"
}


if (count of intersection >= 3) {
  skip line
}

还是我在读你的“匹配”太松散了?

当比较第1行时,这是我的word文件,这是第1行,这是第2行,这是第二行,这是一些文本,你想删除这两行,因为同一个单词出现了3次以上,对吗?然后将第3行与前一行进行比较(已删除)或者是第4行;或者是所有的常用词都被存储了,将来的任何一行都应该被删除吗?你明白我的意思了,兄弟,我想把第1行的词和所有的行进行比较,然后把第2行的词和下面的所有行进行比较,这样下去……但我也不想删除那些词出现1次的唯一行(表示我希望保留仅出现1次且该行唯一的行)…所以输出也应该包括第一行,我必须编辑我的帖子,请检查输出…哇,太接近了,谢谢,但我有一些疑问:当我测试这些行的文本时:,得到了这个输出:这是问题,不知道为什么它不删除最后两行…我不想删除第一行,请e查看我的主要帖子中的编辑(输出)…它会删除包含大写和小写字母的行吗?@user3510922是的,它区分大小写。如果你不想这样做,只需将单词数组的键大小写为大写或小写。然后
One
将等于
One
。添加它之前:
foreach($word的单词){$word=strtolower($word)…
我真的不能理解这个逻辑(isset后面的最后几行),你能用一种简单的方式解释一下吗……非常感谢:foreach($line作为$line){$words=explode(“,$line);foreach($words作为$word){if(isset($result[$word])$result[$word[$word][]=$line;else$result[$word]=array($line);}}}在这里,我只是检查数组
result
是否已经包含名为
$word
的键。根据这一点,我正在创建一个数组,或者将其附加到现有数组中。我已经修改了该行以满足您的要求。
jhasg djgha sdgasjhgdjasgh jdkh
sdhgfkjg sdjhgf sjkdghf sdhf
s hdg fjhsgd fjhgsdj gfj ksdgh
foreach ($words AS $word){
   if (isset($result[$word])){
       if (!in_array($line, $result[$word])){
          $result[$word][] = $line;
       }
   }else
       $result[$word] = array($line);  
}
php > $l1 = 'hello this is my word file and this is line number 1';
php > $l2 = 'hello this is second line and this is some text';
php > $a1 = explode(" ", $l1);
php > $a2 = explode(" ", $l2);
php > var_dump(array_intersect($a1, $a2));
array(7) {
  [0]=>
  string(5) "hello"
  [1]=>
  string(4) "this"
  [2]=>
  string(2) "is"
  [6]=>
  string(3) "and"
  [7]=>
  string(4) "this"
  [8]=>
  string(2) "is"
  [9]=>
  string(4) "line"
}


if (count of intersection >= 3) {
  skip line
}