2016-07-23 50 views
0

我尝试将多行str保存为txt格式,然后使用gets函数进行访问,因此我试图用其他方法替换所有的\ n #n为例)。 由于某种原因,它不起作用。 即使我在不​​同的搜索条件下测试str_replace函数,它似乎也不起作用。 有什么想法为什么?PHP:将多行字符串保存为单行,以便使用str_replace保存为表单

<?php 
     //define variables 
     $docRoot = $_SERVER["DOCUMENT_ROOT"]; 
     $title = $_POST["title"]; 
     $author = $_POST["author"]; 
     $content = $_POST["content"]; 
     $date = time(); 

     //building post 
     str_replace("\\n", "#n", $content); 
     $post = $date.' \t '.$title.' \t '.$author.' \t '.$content."\n"; 

     //open and writing posts file 
     file_put_contents("$docRoot/ToDoProject/posts/posts.txt", $post, FILE_APPEND); 

     echo "<h1>Post uploaded!</h1>" 
    ?> 

后从获取数据:

<form class="form-horizontal" action="php/processpost.php" method="post"> 
    <fieldset> 

    <!-- Form Name --> 
    <legend>Submit post</legend> 

    <!--Date --> 
    <?php 
     $date = date("H:i, dS F, Y"); 
     echo "$date"; 
    ?> 

    <!-- Text input--> 
    <div class="form-group"> 
    <label class="col-md-4 control-label" for="title">Title</label> 
    <div class="col-md-4"> 
    <input id="title" name="title" type="text" placeholder="" class="form-control input-md" required=""> 

    </div> 
    </div> 

    <!-- Text input--> 
    <div class="form-group"> 
    <label class="col-md-4 control-label" for="author">Author</label> 
    <div class="col-md-4"> 
    <input id="author" name="author" type="text" placeholder="hephaestus" class="form-control input-md" required=""> 

    </div> 
    </div> 

    <!-- Textarea --> 
    <div class="form-group"> 
    <label class="col-md-4 control-label" for="content">Content</label> 
    <div class="col-md-4">      
    <textarea class="form-control" id="content" name="content"></textarea> 
    </div> 
    </div> 
    <!--Submit Button --> 
    <button class="btn btn-primary btn-lg" type="submit" value="Submit Order" style="position: absolute; right: 2px;"> 
    Submit</button> 
    </fieldset> 
    </form> 

还,什么时候使用str_replace函数,当prag_replace? 如何在prag_replace中输入相同的代码?

+0

尝试用'“\ n”'替换'“\\ n”'。 –

+0

尝试过,并没有奏效 –

回答

1

的错误是在这一行:

//building post 
str_replace("\\n", "#n", $content); 

您与#N代替换行字符,但你不结果保存回$内容。试试这个:

$content = str_replace("\\n", "#n", $content);