简体   繁体   中英

JavaScript replace <br> with \n

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.... 

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");

If you want to simply remove those characters in your PHP you could use the handy strip_tags() function. This however will remove all HMTL elements in your string.

If you want to simply convert the <br/> string back to a \\n then you can use php's str_replace() function.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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