It is impossible to say what the appropriate solution is for the specific case of the questioner, but the original data are usually kept in the database as presented in the question.
In the database you are saved as "Plain/text". PHP has nothing to do with the fact that it doesn’t "break the line automatically in HTML" because the data is in "plain text" Plain/text.
The question is an old one, but I will demonstrate other means to other people who may have the same doubt and need solutions different from those that were presented.
textarea
A problem of trying to solve by overwriting in the database by <br>
is when you need to display inside a textarea, for example:
<?php
$str = '0 Anos\n3 Dias';
?>
<textarea><?php echo $str;?></textarea>
A textarea will display the Plain/text format and the line breaks will be interpreted.
<textarea>0 Anos
3 Dias</textarea>
But if the text already comes with the tag <br>
from the database, the textarea will literally display the text:
<textarea>0 Anos<br>3 Dias</textarea>
If you print outside a textarea, in the browser interface you will see a result without breaking line:
<?php
$str = '0 Anos\n3 Dias';
?>
<div><?php echo $str;?></div>
Will result in 0 Anos3 Dias
, in one line.
But note the result of the source code (press CTRL+U in Chrome). You will see the result in Plain/text:
0 Anos
3 Dias
Using the tag <pre>
You can solve this in many ways. A simple way is with the HTML resources themselves. The tag <pre>
, interprets line breaks ( n or r)
<?php
$str = '0 Anos\n3 Dias';
?>
<pre><?php echo $str;?></pre>
The visual result of the browser will be this, even without the tag <br>
:
0 Anos
3 Dias
Converting with Javascript
If you want to convert the line breaks into <br>
, can save server side (PHP) processes by using Javascript to convert.
I recommend a function of phpjs.org:
function nl2br (str, is_xhtml) {
var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br ' + '/>' : '<br>' // Adjust comment to avoid issue on phpjs.org display
return (str + '')
.replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag + '$2')
}
Use is identical to function nl2br() of PHP.
Interpreting with CSS
Be aware that the operation depends on the browser version:
<style>
p {
white-space: pre;
}
</style>
<p><?php echo '0 Anos\n3 Dias';?></p>
Consult: https://developer.mozilla.org/en-US/docs/Web/CSS/white-space
It broke the line perfectly, the problem is that php does not respect this line break :/ it prints everything on the same line... Only this is another problem. I ask a new question or we can proceed here?
– GGirotto
Depends on what you’re doing. If to use to put in an HTML page is the first case I spoke in the reply, puts something that is HTML language convention, ie the line break is
<br>
, as Bacco spoke in his reply.– Maniero
@Guilherme PHP has the nl2br() function that does this automatically (changes CHAR(10) or " n" to <br> on the screen). I hadn’t put it in my answer because you didn’t mention PHP in the question. When so, try to explain the details better because the answers may come out more elaborate. I have seen that many of your questions are being resolved in the comments, for lack of details. If you can, ask a little more question so people can ask more in the answer ;)
– Bacco
Sorry, I edited the question and I think it became clearer..
– GGirotto
With the edition worked perfectly. Thank you very much for the help and sorry for the questions, I swear I will try to improve in the next :)
– GGirotto