用PHP提交的文本中的换行符转换 [英] Newline Conversion in Submitted Text in PHP

查看:104
本文介绍了用PHP提交的文本中的换行符转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了一个系统,供用户将其文章提交到我的数据库中.由于它只是HTML,所以我不希望他们每次换行时都知道键入<br />,所以我在输入中使用PHP函数nl2br().

I have a system set up for users to submit their articles into my database. Since it will just be HTML, I don't want to expect them to know to type <br /> every time there's a newline, so I am using the PHP function nl2br() on the input.

我还提供了一个文章修改工具,该工具可以将他们的文章重新带到表单中(但是,这是另一个页面),并允许他们进行编辑.这样,<br />元素也会出现(仍带有换行符).为了纠正出现的
元素(无论如何,这是我所期望的),我添加了preg_replace('/<br(\s+)?\/?>/i', "\n", mysql_result($result,$i,"content")),这是我在此站点上的另一个问题中发现的.它确实完成了删除<br />元素的工作,但是由于将其替换为换行符,并且换行符原本仍会保留,因此每次编辑该帖子时,都会添加越来越多的换行符,使段落之间的间距更大每次都更多.这是用户不会理解的东西.

I'm also providing an article modification tool, which will bring their articles back into the form (this is a different page, however) and allow them to edit it. In doing this, the <br /> elements were appearing also (with newlines still). To remedy the
elements appearing (which I had expected, anyway) I added preg_replace('/<br(\s+)?\/?>/i', "\n", mysql_result($result,$i,"content")) which I had found in another question on this site. It does the job of removing the <br /> elements, but since it is replacing them with newlines, and the newlines would have remained originally anyway, every time the post is edited, more and more newlines will be added, spacing out the paragraphs more and more each time. This is something a user won't understand.

例如,假设我在文章提交表单中输入以下内容:

As an example, say I enter the following into the article submission form:

 Hello, this is my article.
 I am demonstrating a new line here.

这将转换为:

Hello, this is my article.<br />
I am demonstrating a new line here.

请注意,即使换行符已转换,文本中仍然有换行符.在编辑表单中,<br />将转换回换行符,如下所示:

Notice that, even though the newline character was converted, there is still a newline in the text. In the editing form, the <br /> will be converted back to newline and look like this:

Hello, this is my article.

I am demonstrating a new line here.

因为<br />已转换为换行符,但是已经有换行符.所以我想我期望它最初会被转换成这样的东西:

Because the <br /> was converted to a newline, but there was already a newline. So I guess what I'm expecting is for it to originally be converted to something like this:

Hello, this is my article.<br />I am demonstrating a new line here.

我想知道...有没有办法阻止nl2br()函数维护原始换行符?可能与Windows \r\n字符有关吗?

I'm wondering ... is there a way to stop the nl2br() function from maintaining the original newlines? Might it have to do with the Windows \r\n character?

推荐答案

您正在使用的函数nl2br用于插入它们,而不是替换它们.如果要将\n替换为<br />,则只需使用str_replace.像这样:

The function you're using, nl2br is used for inserting them, but not replacing them. If you want to replace \n with <br /> you just need to use str_replace. Like so:

$string = str_replace("\n","<br />",$string);

在这种情况下,绝对不需要正则表达式.

There is absolutely no need for regex in this situation.

这篇关于用PHP提交的文本中的换行符转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆