A question comes up here... where are you inserting this text (string) ?
If you are injecting it as HTML you'll get the desired result in the rendered page.
I assume this is not the case: you want HTML line breaks turned into newlines.
So...
nl2br()
converts newlines into <br />
: that's the opposite you want to do
http://php.net/manual/en/function.nl2br.php
Just use str_replace
:
$out = str_replace( "<br/>", "\n", $in );
Where $in
is the input string and $out
is the desired output
http://php.net/manual/en/function.str-replace.php
Just a couple of things to note:
1) the above code will work with HTML line breaks <br/>
, not if you have <br>
, or <br />
If this is an issue you may pass the function array of strings and array of their replacements. This is well documented in the link above.
2) If you use the code snipped I wrote above you'll end having two spaces in the resulting string:
What is
(with trailing space)
PHP
(with leading space)
PHP?` there's no new lines. – u_mulder May 11 '17 at 06:33
PHP?` – Shibbir Ahmed May 11 '17 at 06:33
PHP?"; echo htmlentities($string);` If you want to show `
` – Sahil Gulati May 11 '17 at 06:33
PHP?` and hope when you are fetching it and you will get the same value like: `What is
PHP?` if this is so then i think you don't have to do any thing just like: `$string = "What is
PHP?"; echo $string;` echo will read the
tag – lazyCoder May 11 '17 at 06:44
PHP?` – Shibbir Ahmed May 11 '17 at 06:48
` when I insert the text into the database but when I showing it no line break. But using `\n` I see a line break in the source code only. – Shibbir Ahmed May 11 '17 at 07:25