Newline Conversion in Submitted Text in PHP

可紊 提交于 2019-12-31 00:50:13

问题


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.

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.

This will convert to:

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

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.

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.

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?


回答1:


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.




回答2:


It seems like the problem you described is not a bug, but a feature of bl2br. You could just write your own function for it, like:

<?php
function NlToBr($inString)
{
    return preg_replace("%\n%", "<br>", $inString);
}
?>

I found this one in the comments of the documentation of the nl2br-function in the PHP Manual: http://php.net/manual/de/function.nl2br.php. If the one I posted did not work for you, there should be plenty more where it came from.

(Or just use the function from the other Answer that was just posted, I guess that should work, too)




回答3:


This should fix it:

preg_replace('/<br(\s+)?\/?>(?!\s*\n)/i', "\n", mysql_result($result,$i,"content"))

You cannot simply remove the breaks, because they might be on the same line. This regex will replace all breaks with newline but not those that are followed by the newline.

It will leave the <br>\n in the text. Additional regex will get rid of them:

preg_replace('/<br(\s+)?\/?>/i', "", $res)


来源:https://stackoverflow.com/questions/9454732/newline-conversion-in-submitted-text-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!