dongyu9263 2014-08-04 16:14
浏览 80

如果存在PHP,如何替换文件中的行

So, I have a code that based on user's input write data to the file. Basically user select the date and the workout which on submit get written to the file. When I try to set it up to check if the string (date) already exist in the file I cannot make it work so that existing line is replaced.

Current code that is writing user's input to the file:

<?php
   include 'index.php';
   $pickdate = $_POST['date'];
   $workout = $_POST['workout'];
   $date = '   \''.$pickdate .'\' : \'<a href="../routines/'.$workout.'" target="_blank"><span>'.basename($workout,'.txt').'</span></a>\',' .PHP_EOL;
   $file = 'test.js';

   // Open the file to get existing content
   $current = file_get_contents($file);

   // Append a new workout to the file
   $current .= $date;
   $current = preg_replace('/};/', "", $current);
   $current = $current.'};';

   // Write the contents back to the file
   file_put_contents($file, $current);
   header("location:index.php");
?>

My attempts were with if statement but again I couldn't manage to write the code that will replace the line with if exists. This is what I have:

<?php
   include 'index.php';
   $pickdate = $_POST['date'];
   $workout = $_POST['workout'];
   $date = '   \''.$pickdate .'\' : \'<a href="../routines/'.$workout.'" target="_blank"><span>'.basename($workout,'.txt').'</span></a>\',' .PHP_EOL;
   $file = 'test.js';

   // Open the file to get existing content
   $current = file_get_contents($file);

   if (strpos($current, '   \''.$pickdate .'\'') ) {
     #here is where I struggle#
   }
   else {
    // Append a new workout to the file
   $current .= $date;
   $current = preg_replace('/};/', "", $current);
   $current = $current.'};';

   // Write the contents back to the file
   file_put_contents($file, $current);
   }
  header("location:index.php");
?>

Currently it is doing this

08-04-2014 : Chest
08-05-2014 : Legs
08-04-2014 : Back

I want this

Now when user choose August 4th again that line to be replaced with new/same choice of workout depending what user selects.

08-04-2014 : Back
08-05-2014 : Legs

Can someone help with the part where I struggle to make this work. Thank you so much in advance.

  • 写回答

1条回答 默认 最新

  • dongxingguo1978 2014-08-04 17:51
    关注

    As explained by Barmar in the comments:

    $current = trim(file_get_contents($file));
    $current_lines = explode(PHP_EOL, $current);
    
    /* saved already */
    $saved = false;
    
    foreach($current_lines as $line_num => $line) {
        /* either regex or explode, we explode easier on the brain xD */
        list($date_line, $workout_line) = explode(' : ', $line);
        echo "$date_line -> $workout_line 
    ";
    
        if($date == $date_line) {
            /* rewrite */
            $current_lines[$line_num] = "$date : $workout";
            $saved = true;
            /* end loop */
            break;
        }        
    }
    
    /* append to the end */
    if(!$saved) {
        $current_lines[] = "$date : $workout";
    }
    
    file_put_contents($file, implode(PHP_EOL, $current_lines));
    

    So, you explode the file, go thru it, line by line, if found overwrite that line, if not append it to the end of the array, then glue it back together and put it back in the file.

    You'll get the idea.

    Hope it helps.

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂