简体   繁体   English

nl2br() 创建一个额外的新行

[英]nl2br() creates an extra new line

I am trying to insert a description text field into the database.我正在尝试将描述文本字段插入数据库。

I was previously just doing something like this:我以前只是在做这样的事情:

$group_description = mysql_real_escape_string($_POST['group_description']);

But it was creating problems because when I was taking things out of the database, they were being displayed with \n\r strings instead of new lines.但这造成了问题,因为当我从数据库中取出内容时,它们显示为 \n\r 字符串而不是新行。

Here is an example of a page with that problem:以下是存在该问题的页面示例:

http://www.comehike.com/hikes/hiking_group.php?hiking_group_id=48 http://www.comehike.com/hikes/hiking_group.php?hiking_group_id=48

So I tried to fix it by adding nl2br like this:所以我试图通过像这样添加 nl2br 来修复它:

$group_description = mysql_real_escape_string(nl2br($_POST['group_description']));

But that just inserted an extra line:( Here is the example of the current problem:但这只是插入了一个额外的行:(这是当前问题的示例:

http://www.comehike.com/hikes/hiking_group.php?hiking_group_id=50 http://www.comehike.com/hikes/hiking_group.php?hiking_group_id=50

Here is an example of an insert statement I use:这是我使用的插入语句的示例:

$insert_group_sql =  'INSERT INTO hiking_groups( title , group_description ) VALUES ( "'.$group_name.'" , "'.$group_description.'" ) ';

What is the proper way to do this?这样做的正确方法是什么?

Here is the code I use to display the $group_description这是我用来显示 $group_description 的代码

          //Convert all urls to links
          $group_description = preg_replace('#([\s|^])(www)#i', '$1http://$2', $group_description);
          $pattern = '#((http|https|ftp|telnet|news|gopher|file|wais):\/\/[^\s]+)#i';
          $replacement = '<a href="$1" target="_blank">$1</a>';
          $group_description = preg_replace($pattern, $replacement, $group_description);

          $group_description = str_replace("\'" , "'", $group_description );
          $group_description = nl2br($group_description);

          /* Convert all E-mail matches to appropriate HTML links */
          $pattern = '#([0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.';
          $pattern .= '[a-wyz][a-z](fo|g|l|m|mes|o|op|pa|ro|seum|t|u|v|z)?)#i';
          $replacement = '<a href="mailto:\\1">\\1</a>';
          $group_description = preg_replace($pattern, $replacement, $group_description);

First, I suggest that you do as Spudley suggests, keep it in the original form in the database and format it once you display it.首先,我建议您按照 Spudley 的建议进行操作,将其以原始形式保存在数据库中,并在显示后对其进行格式化。

nl2br() should work, so I suggest that you check the input data to see exactly what is being input (which is easier to do if you don't format it before you store it in the DB). nl2br()应该可以工作,所以我建议您检查输入数据以确切了解输入的内容(如果您在将其存储到数据库之前不对其进行格式化,这将更容易做到)。 What it do is it takes \n, \n\r, \r\n and \r and inserts a <br/> instead.它所做的是它需要 \n、\n\r、\r\n 和 \r 并插入一个<br/>代替。 You should check that for instance there is no space in between \n and \r.例如,您应该检查 \n 和 \r 之间是否没有空格。

Also, make sure that you only use nl2br() in one place, since it doesn't replace the new lines, it just inserts the <br/> (that is, if you do nl2br(nl2br($group_description)) you will get two <br/> )另外,确保你只在一个地方使用nl2br() ,因为它不会替换新行,它只是插入<br/> (也就是说,如果你这样做nl2br(nl2br($group_description))你会得到两个<br/> )

Update:更新:

I see in the additional code that when you display the description, you already have a nl2br() .我在附加代码中看到,当您显示描述时,您已经有了一个nl2br() You need to remove one of them, so that you only add the <br/> s once.您需要删除其中一个,以便您只添加<br/>一次。

Also, instead of this:此外,而不是这个:

      $group_description = str_replace("\'" , "'", $group_description );
      $group_description = nl2br($group_description);

Try this:尝试这个:

      $group_description = stripslashes($group_description);
      $group_description = nl2br($group_description);

That should remove all the sanitizing that mysql_real_escape_string() did, which should solve the problem of the \n\r showing in the text.这应该删除mysql_real_escape_string()所做的所有清理工作,这应该解决文本中显示的 \n\r 的问题。

Try trim before passing to nl2br:在传递给 nl2br 之前尝试修剪:

nl2br( trim($_POST['group_description']) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM