JavaScript替换< br>与\ n

JavaScript替换< br>与\ n

问题描述:

我刚刚使用JavaScript和PHP在MySQL数据库中存储了一段文本,并用<br />替换了\n,我现在遇到的问题是当我尝试使用PHP检索文本时将其打印出来作为; <br />

I have just stored a paragraph of text in a MySQL database using JavaScript and PHP and replaced \n with <br />, the problem I'm now having is when I try and retrieve the text using PHP it prints it out as; <br />

Dear Sir/Maddam<br />This is a letter concerning the following;<br />I would like to.... 

好吧,显而易见的解决方案是首先不将\n替换为<br />.

Well, the obvious solution is to just not replace \n with <br /> in the first place.

我不知道您要用哪种语言来消除...中的损害

I don't know what language you're trying to reverse the damage in...

// PHP:
$out = preg_replace("/<br ?\/?>/i","\n",$in);

// JS:
out = input.replace(/<br ?\/?>/ig,"\n");