文本文件 - PHP 代码在写入 TXT 文档时将所有 1 替换为任何内容


text files - php code replaces all 1's with nothing when writing to a txt document

所以,我的代码昨天非常适合我的网站......直到我意识到没有人的(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

&&是一个逻辑运算符。

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

如果你&&两个计算结果为(布尔(true的字符串,结果将是true。转换回字符串将1.因此,实际上您正在删除所有1

你的问题是str_replace,特别是"''" && "'""。此语句等效于 1 ,因此您的str_replace实际上是str_replace(1, "" , $title);的。

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

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

这将按照您的预期将数组元素的所有出现替换为空字符串。

我可能发现了你的问题:

您不能像这样使用此函数:

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

("''" && "'"")计算结果为TRUE为布尔值,"1"为字符串。

将数组传递给函数:

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