dongzhi6905 2015-08-26 08:27
浏览 25
已采纳

为什么str_replace无法正常工作

In my directory I have a list.csv of the form:

RP2015, active
Hope, paused 
Process99, active

I'm writing a php script to allow a web user to switch the different lines from 'active' to 'paused' and back. Unfortunately I've hit a snag. My current code is live at: http://whitewaterwriters.com/Driver/index.php

and it looks like this

     <HTML>
   <Body>

   <?
   if ($_POST != null) {
           $target = $_POST ['sprint'];
           echo "<br>Target was:".$target;
           $replacement=str_replace('active','paused',$target);
           if (strpos($target,'paused') !== false) {
                 $replacement=str_replace('paused','active',$target);
          }       
          echo "<br>Replacement was:".$replacement."<br>";
          $filename = "list.csv";
         $contents = file_get_contents($filename);
          print "<br>contents was:".$contents;
          $new_contents = str_replace($target, $replacement, $contents);
          print "<br>contents became:".$new_contents;
          file_put_contents($filename, $new_contents);
  }       
  ?>      
<br><br>
  <?php
  $row = 1;
  if (($handle = fopen("list.csv", "r")) !== FALSE) {
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
              $num = count($data);
              $row++;
              echo '<form action="index.php" method=post>';
              echo $data[0] . $data[1];
              echo '<button type="submit" value="'. $data[0].", ".$data[1].'"     name="sprint">Pause</button></form><br>';
     }
      fclose($handle);
  }
  ?>
  </body>
  </html>

For some reason the replace is not firing. The event is captured, the correct target and replace strings are (I think) generated, but the replace is NOT coming out. The output I get is:

Target was:RP2015, active
Replacement was:RP2015, paused

contents was:RP2015, active
contents became:RP2015, active

RP2015 active

Can anyone tell me what's going on?

EDIT:

The current list.csv is exactly:

RP2015, active

`

  • 写回答

2条回答 默认 最新

  • dsg435665475 2015-08-26 10:14
    关注

    The thing I had forgotten is that html squashes consecutive spaces together which is why the $target looked like it was fine. Actually:

    $data[0].", ".$data[1]
    

    only needed to be

    $data[0].",".$data[1]
    

    Because there was a space left over from the csv parsing. I'll make the code a bit more robust to that in future.

    For the interested - this helper function I wrote helped me locate the error:

        function toask($inStr){
     $arr1 = str_split($inStr);
        foreach ( $arr1 as $a){
          print ord($a)." ";
          }
          }
    

    It converts a string to a sequence of ascii codes do you can make sure, for example, that quote characters are the ones you think they are...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

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