How to give a MySQL field line break in an HTML page

5

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

For example, instead of saving:

0 anos, 3 dias

Save:

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:

ButtheresultontheHTMLpagewasasfollows:

I'm going to get this record in an HTML page, how do I break the line value in this HTML page? I do not need to break the line in the database as long as it works on the HTML page.

    
asked by anonymous 10.04.2015 / 00:18

3 answers

8

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

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

Try this:

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

This \n is the skip line character, depending on where you use it to work or not. Another possibility:

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

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

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

But think hard if you have no other way to resolve this. There are cases where it makes sense to put the formatting text along with the actual text, but in another it is better to handle this in the application. Of course you would have to have some way of indicating that there is a separation between this data. If it really does not make sense to have these two lines separated into different columns, then you have to use some convention to indicate the separation.

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

    
10.04.2015 / 00:32
4

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 that you use the values correctly at the time of displaying on the screen, otherwise the solution will not be effective.

There are several other ways, such as simply "0 anos,<br>3 dias" if the use is HTML, but to determine the best output, depends on the context you want to use the break. One could choose, for example, to use "0 anos,@3 dias" and change @ to something else on the screen (but it's just another example).

    
10.04.2015 / 00:31
2

It is unfeasible to say which solution is appropriate for the specific case of the question-author, but the original data in the database is usually kept the way you presented it in the question.

The database is saved as "plain / text". PHP has nothing to do with not "breaking the line automatically in HTML" because the data is in "plain text" plain / text .

The question is old, but I will demonstrate other means to other people who may have the same doubt and need solutions other than those presented.

textarea

One problem with trying to resolve replacing in the database with <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 line breaks will be interpreted.

<textarea>0 Anos
3 Dias</textarea>

But if the text already comes with the <br> tag of the database, textarea literally displays 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 a line break:

<?php
$str = '0 Anos\n3 Dias';
?>
<div><?php echo $str;?></div>

It will result in 0 Anos3 Dias , on a single 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 <pre> tag

You can solve this in several ways. A simple way is with the HTML features themselves. The <pre> tag 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 line breaks to <br> , you can save processes on the server (PHP) side by using JavaScript to convert.

I recommend a phpjs.org function:

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')
}

The usage is identical to the nl2br () function of PHP.

Interpreting with CSS

Please be aware that this depends on your browser version:

<style>
 p {
 white-space: pre;
 }
</style>
<p><?php echo '0 Anos\n3 Dias';?></p>

See: link

    
07.04.2016 / 16:39