PHP nl2br无法正常工作

问题描述:

For example, I have:

Lorem Ipsum

Dolor Sir Amet

As the content of $s. Now if I print the result of htmlspecialchars(nl2br($s)) is:

Lorem Ipsum<br />
<br />
Dolor Sir Amet

While the result I want is:

Lorem Ipsum<br /><br /> Dolor Sir Amet

Why the nl2br is not removing the carriage return even though it's already inserting the <br /> ? Or how is the correct usage of nl2br to correctly remove the carriage return? Because this breaks my SQL. I'm running PHP on Apache in Windows 7. Thanks.

例如,我有: p>

  Lorem Ipsum 
  
Dolor Sir Amet 
  code>  pre> 
 
 

作为 $ s code>的内容。 现在如果我打印 htmlspecialchars(nl2br($ s)) code>的结果是: p>

  Lorem Ipsum&lt; br /&gt; 
&lt; br /  &gt; 
Dolor Sir Amet 
  code>  pre> 
 
 

虽然我想要的结果是: p>

  Lorem Ipsum&lt; br /&gt  ;&lt; br /&gt;  Dolor Sir Amet 
  code>  pre> 
 
 

为什么 nl2br code>没有删除回车符,即使它已经插入&lt; br /&gt ; code>? 或者如何正确使用 nl2br code>来正确删除回车? 因为这打破了我的SQL。 我在Windows 7上运行Apache。感谢。 p> div>

to replace all linebreaks to <br /> you should use an extended function,
here it is an example:

<?php 
function nl2br2($string) { 
$string = str_replace(array("
", "", "
"), "<br />", $string); 
return $string; 
} 

Each OS have different ASCII chars for linebreak:

windows =
unix =
mac =