在写入txt文档时,php代码无需替换所有1

时间:2013-02-15 22:46:39

标签: php text-files

所以,我的代码昨天对我的网站来说非常完美......直到我意识到文档中没有人(1)出现!我在整个代码中搜索过,似乎没有任何错误!

我不知道这是PHP(5.3)中的错误,还是其他什么......它只需要修复!

回顾:所有(1)将从未知原因中删除)

我的代码在这里:

  echo "Please wait while we submit your recipe...";

  //getting form data
  $thedate = Date("d-m-Y_H:i:s");
  $title = $_POST['Title'];
  $description = $_POST['Descript'];
  $ingredients = $_POST['Ingredients'];
  $instructions =$_POST['Instructions'];
  $notes = $_POST['Notes'];
  $name = $_POST['Name'];

  //replacement for extra characters
  $title = str_replace("\'" && "\"", "" , $title);
  $description = str_replace("\'" && "\"", "" , $description);
  $ingredients = str_replace("\'" && "\"", "" , $ingredients);
  $instructions = str_replace("\'" && "\"", "" , $instructions);
  $notes = str_replace("\'" && "\"", "" , $notes);
  $name = str_replace("\'" && "\"", "" , $name);
  $title = str_replace("\\", "" , $title);
  $the_recipe_url="RecibaseWaitingList/".$thedate."--".$title.".txt";
  $the_recipe_url = str_replace("'", "" , $the_recipe_url);
  $the_recipe_url = str_replace("\"", "" , $the_recipe_url);
  $the_recipe_url = str_replace("?", "" , $the_recipe_url);
  $the_recipe_url = str_replace(chr(10), "_", $the_recipe_url);

  //bugs show up before or after this 
  echo '...';

    //Checking to see if all required fields are filled
    if($title !== '')
    {
        if($ingredients !=='')
        {
            if($instructions !=='')
            {
                          //writing to the file
                          $writetothefile = fopen($the_recipe_url, 'c');
                          fwrite($writetothefile, $name." submitted this recipe on ".$thedate." \r\n \r\n "); 
                          fwrite($writetothefile, "Title: ".$title." \r\n ");
                          fwrite($writetothefile, $description." \r\n ");
                          fwrite($writetothefile, " \r\n ");
                          fwrite($writetothefile, "Ingredents:\r\n".$ingredients." \r\n");
                          fwrite($writetothefile, " \r\n ");
                          fwrite($writetothefile, "Instructions: \r\n ".$instructions." \r\n ");
                          fwrite($writetothefile, "\r\n ");
                          fwrite($writetothefile, "Notes: \r\n" . $notes . "\r\n \r\n ");
                          fwrite($writetothefile, "Thank you ".$name." for submitting this recipe! \r\n \r\n When you're done with the recipe, just click the back button.");
                          fclose($writetothefile);

                          //echo's for the user to see      
                          echo '<br />Your recibase recipe url is <a href="' . $the_recipe_url . '">' . $the_recipe_url . '</a>.';
                          echo "<br />";
                          echo "<br />";                      
                          echo "Thank you for being so generous with your time and entering your recipe. We hope you enjoy using the rest of the site!< br />"; 
                          echo "<br />";                
                          echo '<a href="http://recibase.musicsuper.org/input.php">Click Here</a> to enter in another recipe.<br />';
                          echo "...and we're done!";
             }else{echo "Go back, and enter the <b><i>Instructions</i></b> of your recipe please!".'<br><br><a href="javascript:history.go(-1);">'.'<--- Go Back</a>';}
          }else{echo "Go back, and enter the <b><i>Ingredients</i></b> in your recipe please!".'<br><br><a href="javascript:history.go(-1);">'.'<--- Go Back</a>';}
      }else{echo "Go back, and enter the <b><i>Title</i></b> of your recipe please!".'<br><br><a href="javascript:history.go(-1);">'.'<--- Go Back</a>';

是什么导致1的消失?为什么会这样?

任何想法都将不胜感激。

如果您希望查看此代码的产品并附带示例配方,请转到此处:http://recibase.musicsuper.org/AllOfYourRecipes/RecibaseWaitingList/15-02-2013_13:38:17--Deer%20Liver%20Pate.txt

3 个答案:

答案 0 :(得分:2)

&&是一个逻辑运算符。

$title = str_replace("\'" && "\"", "" , $title);

如果你&&两个评估为(布尔)true的字符串,结果将是true。转换回字符串的将是1。所以实际上你删除了所有1

答案 1 :(得分:1)

您的问题是str_replace,尤其是"\'" && "\""。此声明等同于1,因此您的str_replace实际上是str_replace(1, "" , $title);

您将要使用数组,因此代码将是

str_replace(array("\'", "\""), "" , $title);

这将用空字符串替换所有数组元素的出现,就像你想象的那样。

答案 2 :(得分:0)

我可能发现了你的问题:

你不能像这样使用这个功能:

str_replace("\'" && "\"", "" , $ingredients);

("\'" && "\"")TRUE评估为布尔值,并将"1"评估为字符串。

将数组传递给函数:

str_replace(array("\'", "\""), "" , $ingredients);