str-replace-PHP:将多行字符串保存为单行,以便使用str_replace保存为表单


str replace - PHP: Saving multiline string as single line in order to save to form using str_replace

我试图将多行str保存到txt格式,然后使用gets函数进行访问,所以我试图用其他东西替换所有''n(例如#n)。由于某种原因,它不起作用。即使我在不同的搜索词上测试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>"
    ?>

Post从获取数据

<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中键入相同的代码?

错误在这一行:

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

您将换行符替换为#n,但不会将结果保存回$content。试试这个:

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