Search code examples
phpmysqltextline-breaksword-wrap

How to wrap text when inporting from MySQL database, not before?


I have a problem related with linebreaks of an inported text, previously inserted in MySQL using the classic PHP - HTML form method.

The problem is the following. I want to load the text saved in MySQL databe into a "news" section in my web page. The thing is that my web page has a PC version and a mobil version, each one with different widths.

So, I don't want to insert linebreaks when I submit the text to MySQL (I mean the line-breaks the form will submit if the text line width excedes a hipotethical "cols" width, assuming I'd do a "physhical" or "hard" wrap; the manually inserted line-breaks to separate parragraphs I want to keep of course) because, as far as I know, you have to specify "cols", which is a parameter that will tell the width of the lines before doing the linebreaks.

This is not interesting, because the text fields on my "news" sections will have different widths, as I've told you, so importing text with linebreaks from MySQL won't adjust the the two different "news" sizes in my web.

What I need is to upload text to MySQL with no linebreaks, and then, let the two "news" sections in my web do the formatting of the text.

I though this would be easy, as I'm just parsing the saved text in MySQL databse into a <div> tag with a specified width. But the thing is that the text overflows the <div> container width every time.

This is what I'm using as the text input in the HTML form:

<textarea name="STORY" wrap="physical">EXAMPLE</textarea> 

To inject the news in MySQL I use the typical PHP:

$var = "INSERT INTO exampleTable ('story') VALUE ($_POST['STORY']);

To load the saved text, I just echo the value of a variable that imports the text from the story field of MySQL database between div tags:

echo "<div>".$story."</div>";

As you can see, because I don't wan´t to use a "hard" wrap when I insert the text from the from in MySQL to avoid inserting line-breaks in the lines that otherwise would exced the "cols" width, I use a "phisycal" wrap, but I don't specify "cols", so I think that should prevent the form from inserting line-breaks other than the ones I do manually (presing "enter" key).

But the resulting text, when I echo it, will overflow my div width, as I've told you before.

Shouldn't the div width wrap the text inside of itself?

Should I delete the wrap="physhical" attribute from the form?


Solution

  • First, please note that you're insertion into the database is very likely to be insecure, and will probably result in SQL Injections.

    To remove every linebreak, you can do things like that:

    echo "<div>".str_replace("\n", "", $story)."</div>";