2010-03-31 71 views
1

當我使用函數nl2br($ text)時,它不顯示我想要的中斷,而是顯示中斷的位置。我爲用戶顯示確認頁面,顯示他們在表單中輸入的詳細信息。爲什麼我的nl2br()函數顯示 r而不是中斷(<br />)? PHP

(the confirmation code in php) 
// Confirm success with the user 
    echo '<p>You have successfully posted a ad.'; 
    echo 'Here is your posting: <br /><br />'; 
    echo 'Title: ' .$title. '<br />' ; 
    echo 'Price $: ' .$price.'<br />' ; 
    echo 'Location: ' .$city. ' ' .$state. '<br />' ; 
    echo '<b>Detail: </b>' .nl2br($detail). '<br />'; 

(The form users fill out) 
<div class="fieldwrapper"> 
<label for="detail" class="styled">Detail:</label> 
<div class="thefield"> 
    <textarea id="detail" name="detail"><?php if (!empty($detail)) echo $detail; ?></textarea> 

(The output) 
This is a test\r\n\r\ntesting is good\r\n\r\nWhy doesn\'t it work? 
+0

謹慎顯示代碼? – 2010-03-31 18:20:19

+0

當我做了一個echo'測試運行詳情:'.nl2br($ detail)。 '
'本身,它似乎工作,並會顯示空格,而不是\ r \ n。只有在這裏它似乎沒有工作 – ggfan 2010-03-31 18:27:34

回答

2
$input = 'this \r\nis what\r\ni want\r\n\r\n\r\nd'; 
echo nl2br(str_replace('\r', '', $input); 

刪除\ r第一,這應該幫助。

+0

非常感謝你!得到它的工作。討厭那些\ r \ n的東西:/ – ggfan 2010-03-31 18:36:01

+0

哈哈沒問題,只要確保沒有人在\ r中鍵入,因爲您正在過濾它。隊友的歡呼聲。 – JonnyLitt 2010-03-31 18:37:38

+0

@ggfan你在開玩笑吧。你真的那麼笨嗎str_replace('\ r','',$ input)? – 2010-03-31 18:44:30

0

\r分別爲已在您的文本不是回車符,而是字面上的2符號序列 - 由r字母形成的反斜線。 您必須在代碼中找到一個將換行符轉換爲\r序列的位置。 mysql_real_escape_string()後可能會做的東西

更新:
在你的評論後,我明白了。 您正在使用單引號來分隔您的字符串。
重新PHP語法,不用擔心。真實的數據會很好。

相關問題