Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php脚本编写的两个txt文件之间的差异_Php_Sorting - Fatal编程技术网

php脚本编写的两个txt文件之间的差异

php脚本编写的两个txt文件之间的差异,php,sorting,Php,Sorting,我已经构建了这个脚本,它应该只保存唯一的行,在本例中是url。在.txt文件中,每行一个url 在我的表单上,输入name=“url”中只有一个值是“url” 脚本工作,获取值,检查是否唯一,写入常量文件,然后应用从A到Z的排序,并将排序后的输出写入新的临时文件。以下是我的三件事,我想请求,任何帮助都表示感谢: 我不确定这是将排序写入新文件的正确方法,还是可以重新写入原始文件 临时文件是在具有值的行之间使用空行写入的。无法从空行的来源找到它 最后一点是临时文件总是比常量文件少一行 <?ph

我已经构建了这个脚本,它应该只保存唯一的行,在本例中是url。在.txt文件中,每行一个url

在我的表单上,输入name=“url”中只有一个值是“url”

脚本工作,获取值,检查是否唯一,写入常量文件,然后应用从A到Z的排序,并将排序后的输出写入新的临时文件。以下是我的三件事,我想请求,任何帮助都表示感谢:

  • 我不确定这是将排序写入新文件的正确方法,还是可以重新写入原始文件

  • 临时文件是在具有值的行之间使用空行写入的。无法从空行的来源找到它

  • 最后一点是临时文件总是比常量文件少一行

    <?php
    
    // config
    $url = $_GET['url'];
    $n = "\n";
    $data = $url . $n;
    $file = "datatest.txt"; // constant file
    $file2 = "datatest2.txt"; // temp file
    
    // check for duplicates & add new value if unique
    $unique_data = file_get_contents($file);
    if(strpos($unique_data, $data) === false){
    $fh = fopen($file, 'a') or die("Can't open the file");
    
    // chmod (optional)
    chmod($file,0666);
    chmod($file2,0666);
    
    // sort
    $lines = file($file);
    sort($lines);
    
    // write to original
    $record = file_put_contents($file, $data, FILE_APPEND | LOCK_EX);
    
    // write to temp
    $record2 = file_put_contents("$file2", implode("\n", $lines));
    
    }
    
    // testing echo the results
    if (empty($record)){
    echo "data already exists </br></br>";
    }else{
    echo $record . " bytes written to file </br></br>";
    echo $record2 . " bytes written to file2 </br></br>";
    }
    
    // display all (change file if needed)
    $fh = fopen($file, 'r');
    $pageText = fread($fh, 25000);
    
    echo nl2br($pageText);
    
    ?>
    
    
    
  • 欢迎您的任何意见

    编辑:

    在@Barmar的帮助下,我成功地消除了第2点和第3点

    <?php
    
    // config
    $url = $_GET['url'];
    $n = "\n";
    $data = $url . $n;
    $file = "temp.txt"; // temp file in local folder
    $file2 = "constant.txt"; // constant counterpart file
    
    // check for duplicates & add new value if unique
    $unique_data = file_get_contents($file);
    if(strpos($unique_data, $data) === false){
    
    $fh = fopen($file, 'a') or die("Can't open the file");
    
    // chmod (optional)
    chmod($file,0666);
    chmod($file2,0666);
    
    // write to original
    $record = file_put_contents($file, $data, FILE_APPEND | LOCK_EX);
    
    // sort
    $lines = file($file, FILE_IGNORE_NEW_LINES);
    sort($lines);
    
    // write to temp
    $record2 = file_put_contents($file2, implode("\n", $lines), LOCK_EX);
    
    }
    
    // testing echo the results
    if (empty($record)){
    echo "data already exists </br></br>";
    }else{
    echo $record . " bytes written to file </br></br>";
    echo $record2 . " bytes written to file2 </br></br>";
    }
    
    // display all (change file if needed)
    $fh = fopen($file2, 'r');
    $pageText = fread($fh, 25000);
    
    echo nl2br($pageText);
    
    ?>
    
    
    
    仍然无法理解如何只使用一个文件@Barmar,我试着使用你的“in_数组”,但它给了我一个错误

  • 是的,可以重写原始文件。只需使用一个始终排序的文件:

    $lines = file($file, FILE_IGNORE_NEW_LINES);
    if (in_array($url, $lines)) {
        $lines[] = $url;
        sort($lines);
        file_put_contents($file, implode("\n", $lines) . "\n");
    }
    
  • 空行来自
    内爆(“\n”,$lines)
    ,因为
    $lines
    中的字符串已经以换行符结尾。所以你在它们之间添加了第二条换行符。在上面的代码中,我使用了
    FILE\u IGNORE\u NEW\u行
    ,以防止它将它们包含在字符串中

  • 我怀疑这与新词有关,但我不太确定


  • 这就叫道,“使用数据库”为什么不可能重写原始文件?@rtfm这个脚本只是更大画面的一部分,有多个mysql数据库,但这个脚本需要使用.txt文件来完成。Ready.txt文件通过(手动和cron)导入到相应的mysql数据库中approach@rtfm,恕我直言,但我要问reg。txt文件解决方案,而不是如果我应该使用mysql或其他类型的db.thx,我会尝试一下,明天会让你知道,我在这里写更多的东西已经晚了。顺便问一下,它是$in_数组还是应该in_数组?我还设法解决了第三点。我更改了代码顺序,并将//sort放在//write temp文件之前,但将//放在常量文件的写入之后。关于第二点你也是对的。“\n”。也修复了。