How do I convert data from a multi-line Mysql Text field to line-breaking HTML?

Asked

Viewed 83 times

1

I have a Text field called INFORMATION in my Mysql table containing data in multiple rows:
Joseph,
Maria,
Melissa,

When I convert to HTML the result appears in a single line: Jose, Maria, Melissa,

I tried to include the tag < br> at the end of each line using a SELECT, but not successful.

I tested:

SELECT INFORMATION FROM my_table WHERE REPLACE(INFORMATION,' r n','< br>');

As a result I got 34 lines changed, but I don’t understand this result, because my table contains more than 2,000 records, and all records have more than three lines in the field INFORMACOES.

Have a similar question on How to break a line of a Mysql field into an HTML page but there teaches how to insert data with break row in the Mysql table. I don’t want to insert, I want to extract them from the table to an HTML page with a break for each row, which are all in the same field.

How do I do it?

1 answer

2

Use the function nl2br in the query result. Example:

<?=nl2br($row['informacoes'])?>

Will add a tag <br /> breaks the lines of the record, resulting in:

José, <br />
Maria, <br />
Melissa,

The problem is that a space is created before the <br />, that you can eliminate them with str_replace:

<?=str_replace(" ", "", nl2br($row['informacoes']))?>

Resulting in:

José,<br />
Maria,<br />
Melissa,

This is if in each row there is only 1 name (no spaces).

If there is a possibility of 2 names on a line, use preg_replace to eliminate only space before the <br />:

<?=preg_replace("/,\s/", ",", nl2br($row['informacoes']))?>

Exit:

José Antônio,<br />
Maria,<br />
Melissa,
  • I found a solution to my problem: (1) I cloned my table, (2) Using an UPDATE I changed the line break ' n r' by '< br>' in the cloned table. (3) I converted the records of the cloned table into HTML, preserving the original table. Everything worked out. (4) Finally I deleted the cloned table. This process was very fast, as I wanted. Anyway thanks for your help. I will test your reply, then I inform here the result.

  • Only now I found time to test the use of the function "nl2br, but I did not find this function. I think it is for PHP, but I use Delphi. Thanks for the tip.

Browser other questions tagged

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