How to break a line of a Mysql field into an HTML page

Asked

Viewed 14,735 times

9

I need to save a value with a line break in the table

For example, instead of keeping:

0 anos, 3 dias

Keep:

0 Anos
3 Dias

I tried the following process:

INSERT INTO tabela (tempo) VALUES ('0 Anos\n3 Dias');

He even broke the code inside the registry:

http://puu.sh/h7Z1D/828fc04040.png

But the result on the HTML page was as follows:

Resultado

I’ll take this record in an HTML page, how do I break the value of the line in this HTML page? I don’t need you to break the line in the database, as long as it works on the HTML page.

3 answers

10


You can store virtually anything in a column of characters. You obviously cannot format these characters, they are "pure". What you can do is put characters that indicate formatting by some convention of your application or the language you are using. So if you want it to indicate that you should skip a line, put a character that indicates this.

Of course depending on what you will do to use this data you may have to do something to get the line skipped, but this is a presentation problem.

Try this:

INSERT INTO tabela (nome) VALUES ('0 Anos\n3 Dias');
SELECT nome FROM tabela

That one \n is the skip line character, depending on where the use will work or not. Another possibility:

INSERT INTO tabela (nome) VALUES ('0 Anos
3 Dias');
SELECT nome FROM tabela

If it is to use with HTML then the ideal would be this:

INSERT INTO tabela (nome) VALUES ('0 Anos<br>3 Dias');
SELECT nome FROM tabela

I put in the Github for future reference.

But think about it if you don’t have another way to handle this. There are cases where it makes sense to put the formatting text together with the actual text, but in other cases it is better to treat this in the application. Of course you would have to have some way of indicating that there is a separation between that data. If it really doesn’t make sense to have these two rows separated into different columns, then you have to use some convention to indicate separation.

Another possibility is to do what Bacco said in the comment and after reading the database that has a \n, uses the function nl2br() PHP to convert normal text line break to HTML line break.

  • 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?

  • 2

    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.

  • 3

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

  • Sorry, I edited the question and I think it became clearer..

  • 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 :)

5

Just store the line breaks:

 UPDATE tabela SET campo = CONCAT( "0 anos,", CHAR(10), "3 dias )

Note that the desired break depends on a number of factors:

 UPDATE tabela SET campo = CONCAT( "0 anos,", CHAR(13), CHAR(10), "3 dias" )

The most important thing is to use the values correctly when showing on screen, otherwise the solution will not be effective.

There are several other ways, like simply "0 anos,<br>3 dias" if the use is HTML, but to determine the best output, it depends on the context you want to use the break. Someone could choose, for example, to use "0 anos,@3 dias" and change the @ by something else on the screen` (but it’s just one more example).

3

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.