【问题标题】:PHP + PDO, my escaped characters won't show when displayedPHP + PDO,我的转义字符在显示时不会显示
【发布时间】:2012-07-04 16:27:52
【问题描述】:

首先,我要道歉,我还是个初学者。

出于学习目的,我正在创建一个博客引擎,我只是注意到,当我列出 cmets 时,数据库中正确显示了回车符(按 Enter)等转义字符,但在显示 cmets 时只有一个空格字符。

我使用的是 PostgreSQL 8.3。

在这里您可以看到一个示例数据库条目:

fema=> select * from comments where id = 54;
-[ RECORD 1 ]-------------------------
id       | 54
authorid | 1
text     | This new line won't show.\r
        : No\r
        : \r
        : new\r
        : \r
        : \r
        : \r
        : lines.
time     | 1341417673
postid   | 15
answerid | 0

在这里你可以看到 var_dump() 显示的内容:

string(50) "This new line won't show. No new lines." 

这就是我获取数据的方式:

$stmt = db::$db->prepare("select comments.id as commentid ,authorid,text,time,postid,answerid,users.* from comments left join users on users.id = comments.authorId where postid = :postId");
            $stmt->execute(array('postId' => $_GET['p']));
        $commentRslt = $stmt->fetchAll();

然后 foreach 遍历它们并替换我用来识别我必须替换的东西的标记:

$currComment = str_replace('{{{cms:comment:text}}}', $commentRslt[$key]['text'], $currComment);

这就是我将新评论插入数据库的方式:

$stmt = self::$db->prepare('INSERT INTO comments (authorId, text, time, postId, answerId) VALUES (:authorId, :text, :time, :postId, :answerId)');
$stmt->execute(array(   'authorId' => $_SESSION['userId'],
                        'text' => str_replace(array('<','>'), array('&lt','&gt'), isset($_POST['newCommentArea']) ? $_POST['newCommentArea'] : $_SESSION['newCommentArea']),
                        'time' => time(),
                        'postId' => isset($_POST['commentNew']) ? $_POST['commentNew' ] : $_SESSION['postId'],
                        'answerId' => $answerId));

抱歉有很多代码示例,但我什至不知道问题出在哪里,我想彻底。

那么谁能告诉我如何解决这个问题?如果只是告诉我我在哪里犯了错误。我真的一点头绪都没有。

谢谢。

【问题讨论】:

    标签: php postgresql pdo


    【解决方案1】:

    这里只是猜测,但是:

    HTML/浏览器将连续的空格折叠成一个空格。如果你想保留它们,用&lt;br&gt;标签替换换行符,使用nl2br

    【讨论】:

    • 谢谢,现在可以了。没想到会这么容易。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-26
    • 2011-08-06
    • 1970-01-01
    相关资源
    最近更新 更多