2010-03-31 58 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语法,不用担心。真实的数据会很好。

相关问题