dongzao9044 2018-01-17 21:07
浏览 39
已采纳

php脚本编写的两个文本文件之间的区别

I have build this script that should save only unique lines, in this case url's. One url per line into a .txt file.

On my form I have only one value which is "url" from input name="url".

Script works, get's the value, checks if unique or not, writes into constant file , then I applied sorting from A to Z and writes sorted output into a new temp file. Here are my 3 things that I would like to ask for, any help appreciated:

  1. I'm not sure if this is correct approach to write sorting into new file or is it possible to re-write the original file

  2. Temp file is written with empty lines in between lines with values. Can't figure it out from where empty lines are coming from.

  3. Last bit is that temp file is always one line less than constant file.

    <?php
    
    // config
    $url = $_GET['url'];
    $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("
    ", $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);
    
    ?>
    

Any input appreciated.

edit:

With @Barmar help I managed to eliminate points 2 & 3.

<?php

// config
$url = $_GET['url'];
$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("
", $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);

?>

Still can't figure it out how to use only one file instead. @Barmar, I've tried to use your "in_array" but it's giving me an error.

  • 写回答

1条回答 默认 最新

  • dongpo2002 2018-01-17 21:19
    关注
    1. Yes, it's possible to rewrite the original file. Just use a single file that's always sorted:

      $lines = file($file, FILE_IGNORE_NEW_LINES);
      if (in_array($url, $lines)) {
          $lines[] = $url;
          sort($lines);
          file_put_contents($file, implode("
      ", $lines) . "
      ");
      }
      
    2. The empty lines are from implode(" ", $lines), because the strings in $lines already end with newline. So you're adding a second newline between them. In my code above I used FILE_IGNORE_NEW_LINES to prevent it from including them in the strings.

    3. I suspect this has something to do with the newlines, but I'm not quite sure.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算