Johnathan Nixon
Johnathan Nixon

Reputation: 23

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

Upvotes: 2

Views: 4203

Answers (2)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324650

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

Upvotes: 5

Lix
Lix

Reputation: 47976

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

Upvotes: 1

Related Questions