0

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?

double-beep
  • 5,031
  • 17
  • 33
  • 41
Pedro Peña
  • 49
  • 1
  • 10
  • Using `cols` will not magically insert line-breaks on the submitted data. What linebreaks are you talking about? – Yoshi Mar 18 '16 at 09:45
  • Hi yoshi. You are right. I´m talking about the line-breaks that the "wrap" attribute will insert if the submited text excedes the hipotthical "cols" width. But, I want to keep the manually inserted libe-breaks to conserve the parragraphs. – Pedro Peña Mar 18 '16 at 10:15
  • That's what I mean. You don't have to do anything. The text will **only** contain those linebreaks entered manually. Irregardles of the configuration of the textarea-element. Meaning, in your database you'll only have the former mentioned linebreaks. And if you display that text inside a `
    ` or similar, it will span the whole available width. To *make the* conscious linebreaks *visible*, simply use [`nl2br`](https://secure.php.net/manual/function.nl2br.php) while outputting the text. And to answer your added quesiton, yes, by all means don't use any fancy attribute on the form.
    – Yoshi Mar 18 '16 at 10:37

3 Answers3

1

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>";
blue112
  • 52,634
  • 3
  • 45
  • 54
  • I was going to post the same :) However, I wouldn't go near `cols` to determine the width of a textarea field, especially as this could be dynamic. Use css and `width`. – Egg Mar 18 '16 at 09:46
  • Yeah, that´s why I´m not using "cols". But the resulting text excedes the
    width, no matter if I specify the width with css. That´s the problem.
    – Pedro Peña Mar 18 '16 at 10:17
  • 1
    Hi there. Just wanted to thank you for the answer blue112. I followed your example and used some code to handle the linebreaks, replacing \n in the string. I used 'htmlspecialchars' and then 'str_replace' and that did it. I can retrieve my linebreaks flawlessly from now on. Turns out, by the way, my 'pre' tags, which I didn´t write in my post but were in the original code, were making a tough fight against my attemps to wrap the rendered text. So if anyone is having the same problems, start first with not using 'pre' tags to render retrieved texts. – Pedro Peña Apr 21 '16 at 11:47
0

You could try handling this in MySQL itself using the TRIM() function:

INSERT INTO exampleTable ('story') VALUE TRIM(TRAILING '\n' FROM $_POST['STORY'])
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

You shouldn't insert line breaks yourself - CSS can do this for you. Here is a SO answer that appears to do everything you might want to do.

Community
  • 1
  • 1
Neville Kuyt
  • 29,247
  • 1
  • 37
  • 52