如何检查txt文件中的重复内容?

时间:2019-02-11 09:08:54

标签: php

我有一个.txt文件,每行包含对象。第一行是“婴儿”,第二行是“幼儿”,下一行是“狗”,后一行是“猫”。

我希望能够使用php将对象添加到我的txt文件中,但我想防止重复。以某种方式我的代码仅适用于“ dog”。当我尝试添加“ dog”时,它将说该对象已经存在,但是当我尝试“猫” /“婴儿” /“幼儿”时,即使它已经在列表中,它仍然会添加。

代码

$check = false;
    if(isset($_POST['add'])){
        if($_POST['addLbl'] == ''){
            echo ' Please enter a label';
            $check = true;
        }
        else{
            $data = "\r\n".$_POST['addLbl'];
            $file_lines = file('lbls/predefined_classes.txt');

            foreach($file_lines as $line){
                if($_POST['addLbl'] === $line){
                    $check = true;
                }
            }
            if($check === false){
                $ret = file_put_contents('lbls/predefined_classes.txt',$data,FILE_APPEND | LOCK_EX);
                    if($ret === false){
                        echo ' Unable to add.An error occurred.';
                    }
                    else{
                        echo ' Sucessfully added!';
                        $_POST['addLbl'] = '';
                    }
            }else{
                echo 'The label '.$_POST['addLbl'].' already exists.';
            }

        }

Txt File Content

4 个答案:

答案 0 :(得分:0)

因为您是手动添加列表,并且每行末尾都有空格,所以添加trim()函数可以解决问题

while ($row = mysql_fetch_assoc($data)) { ... }

cat的输出测试:

<?php

$check = false;
if(isset($_POST['add'])){

    if($_POST['addLbl'] == ''){
        echo ' Please enter a label';
        $check = true;
    }
    else{
        $data = "\r\n".$_POST['addLbl'];
        $file_lines = file('lbls/predefined_classes.txt');
        foreach($file_lines as $line){
            if($_POST['addLbl'] === trim($line)){ // trim() added here
                $check = true;
            }
        }
        if($check === false){
            $ret = file_put_contents('lbls/predefined_classes.txt',$data,FILE_APPEND | LOCK_EX);
                if($ret === false){
                    echo ' Unable to add.An error occurred.';
                }
                else{
                    echo ' Sucessfully added!';
                    $_POST['addLbl'] = '';
                }
        }else{
            echo 'The label '.$_POST['addLbl'].' already exists.';
        }

    }
}
    ?>

答案 1 :(得分:0)

尝试以下操作,用相同的逻辑短一些。除其他改进外,无需$check

    if(isset($_POST['add'])){
        if($_POST['addLbl'] == ''){
            echo ' Please enter a label';
        }
        else{
            $data = "\r\n".$_POST['addLbl'];
            $file_str = file_get_contents('lbls/predefined_classes.txt');

            if (!strrpos ( $file_str , $data)) { // not found
                $ret = file_put_contents('lbls/predefined_classes.txt',$data,FILE_APPEND | LOCK_EX);
                    if($ret === false){
                        echo ' Unable to add.An error occurred.';
                    }
                    else{
                        echo ' Sucessfully added!';
                        $_POST['addLbl'] = '';
                    }
            }else{
                echo 'The label '.$_POST['addLbl'].' already exists.';
            }
        }
    }

答案 2 :(得分:0)

您将获得带有换行符的行。读取文件并将其分成几行。

$newline = PHP_EOL;  // your editor might use another new line character
if(in_array($_POST['addLbl'], explode($newline, file_get_contents('test.txt'))))
  // echo error message
else
  // append to file

整个任务可以完成几行。顺便说一句。还要检查未设置的$_POST条目。

if(isset($_POST['add']))
{
  if(!isset($_POST['addLbl']) || $_POST['addLbl'] === '')
    echo ' Please enter a label';
  elseif(in_array($_POST['addLbl'], explode(PHP_EOL, file_get_contents('lbls/predefined_classes.txt'))))
    echo "The label {$_POST['addLbl']} already exists.";
  elseif(false === file_put_contents('lbls/predefined_classes.txt',PHP_EOL . $_POST['addLbl'], FILE_APPEND | LOCK_EX))
    echo ' Unable to add.An error occurred.';
  else
  {
    echo ' Sucessfully added!';
    $_POST['addLbl'] = '';
  }
}

答案 3 :(得分:0)

The $file_lines array looks like as below
array
(
[0] => \r\n
[1] => baby\r\n
[2] => toddler\r\n
[3] => cat\r\n
[4] => dog
)

您必须从$ line中删除下一行转义序列(即\ r \ n),然后与$ _POST ['addLbl']比较