2012-07-04 40 views
0

首先,我想道歉,我還是個初學者。PHP + PDO,我的轉義字符在顯示時不會顯示

爲了學習的目的,我創建了一個博客引擎,我剛剛注意到,當我列出註釋時,轉義字符(如回車符)正確顯示在數據庫中,但顯示註釋時只顯示一個空白字符。

我正在使用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)); 

很抱歉的許多代碼示例,但我不知道問題出在哪裏,我想徹底。

所以任何人都可以請告訴我如何解決這個問題?如果只是告訴我我犯了什麼錯誤。我真的不知道。

謝謝。

回答

3

突出部分猜在這裏,但:

連續的空格由HTML /瀏覽器壓縮成一個空間。用<br>替換換行符,如果你想保留它們,使用nl2br

+0

謝謝,它現在有效。沒想到會這麼容易。 :) –