简体   繁体   中英

How to add line-breaks to a textarea with text over more lines

I am using a textarea width about 300px with, 2 rows and a maxlength.

If the text is two rows long I do not receive any line breaks ( <br/> or \\n ) in the $_POST var.

PHP var_dump output of the textarea $_POST:

string(35) "test test test test test test test "

HTML:

<textarea name="text-1" id="text-1" rows="2" maxlength="35">Click here to enter text</textarea>

CSS:

#text-1, #text-2 {
    background: none;
    border: none;
    width: 100%;
    max-width: 228px;
    height: 100%;
    word-wrap: break-word;
    text-align: center;
    resize: none;
    font: bold 21px 'Impact', sans-serif;
    color: #fff;
    text-shadow:  2px  2px 2px black,
                  2px -2px 2px black,
                 -2px  2px 2px black,
                 -2px -2px 2px black;
}

I want to achieve this output depending on where the natural breakpoint into the second row is:

string(35) "test test test test test\n test test "

Any ideas if thats possible?

You can add the attribue wrap="hard" and it will send a newline wherever it wraps:

<textarea wrap="hard" name="text-1" id="text-1" rows="2" maxlength="35">Click here to enter text</textarea>

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/textarea

Edit:

the cols attribute must be specified.

You can approximate it if you use the wordwrap function

$out = wordwrap($string, 70);

or, if you need HTML breaks

$out = wordwrap($string, 70, "<br />\n");

PS Make sure you're sanitizing the input before hand so you're not vulnerable to XSS and other attacks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM