Line break error in <textarea>

Asked

Viewed 2,005 times

1

Well I have a field in Mysql database of type march 255.

I set up a textarea like this:

<textarea name="obs" maxlength="255"></textarea>

If I put 255 character inside it, I can save without any problems. But if I delete a character and skip a line I can’t save. Does anyone know how to resolve this? remove line breaks?

I’m catching him like this:

$obs = addslashes(filter_input(INPUT_POST, 'obs', FILTER_SANITIZE_SPECIAL_CHARS));

--------- I edited ------

I noticed that for each line break, it saves this value in the comic &#13;&#10;

  • After deleting a character and skip a line and try to save what happens? If an error occurs, copy and paste the error content here for a better understanding of the question, please.

  • the error is that Data Too long for column 'Obs' at Row 1

  • Make a check on the variable that stores the contents of textarea for the content size before trying to save to the bank. Before that you can even try to remove line breaks with $textarea = preg_replace("/[\n\r]/", "", $textarea) or cut content to check what is left out with $textarea = substr ($textarea , 0, 255)

  • good if I use the substr works, it cuts 3 letters for each line break, the problem and that doesn’t look cool. And your way of removing breaks with preg_replace did not work

  • with 3 Replaces you solve your problem, see in my answer;

4 answers

2


I used the following code in HTML+PHP to test:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <?php
    if (isset($_POST['textarea'])) {
      $raw_textarea = filter_input(INPUT_POST, 'textarea');
      $raw_textarea_length = strlen($raw_textarea);
      $textarea = preg_replace("/([\n\r]+|[\s]{2,})/", "", nl2br($raw_textarea));
      $textarea_length = strlen($textarea);
    } else {
      $raw_textarea = "";
      $raw_textarea_length = 0;
      $textarea = "";
      $textarea_length = 0;
    }
    ?>
    <form action="" method="POST">
      <input type="number" value="<?=$raw_textarea_length?>"> <br>
      <input type="number" value="<?=$textarea_length?>"> <br>
      <input type="text" value="<?=$textarea?>"> <br>
      <textarea name="textarea" rows="8" cols="80" maxlength="128">
        Alguma coisa acontece no meu coração
        Só quando eu cruzo a Ipiranga
        Com a avenida São João
  alsjkdna</textarea> <br>
      <input type="submit" name="submit" value="Enviar">
    </form>
  </body>
</html>

And I could see that:

  • Although the field is limited to 128 characters, it was containing 132 characters and I was still able to write 4 more even if they were removed when clicking on Submit, IE, can not trust completely the attribute maxlength
  • Using the function nl2br considerably increases the length of the unfilled string <br />, 6 characters before each \n or \r\n which are only 2 or 4 characters respectively
  • Removing extra spaces and replacing line breaks considerably reduces the size of the string with preg_replace("/([\n\r]+|[\s]{2,})/", "", nl2br($raw_textarea));

I suggest that you increase the size in the field in Mysql or reduce the page and, if you need accuracy, control the size of the string within the textarea with Javascript to limit the amount of information to be allowed before shipping.

------------------ EDIT ------------------

With the change in your question that informs you about these special characters when saving to the bank, use preg_replace("/([\n\r]+|[\s]{2,})/", "", nl2br($raw_textarea)); so that the bank does not need to escape the special line break characters and its problem should be solved.

1

How do I solve the problem immediately?

It is not a gambit, but an option, after all, a gambit is much worse than that. You have the option to save in the database with line breaks, including I used this technique a lot and it works, depending on your need, if you want to complicate the process too much, or do something simplified and functional. With STR_REPLACE looking to replace each line break with the part you want saved:

For study purposes, understanding the function used

Understanding the str_replace function used

The str_replace() function replaces some characters with others characters in a string.

This function works by the following rules:

  1. If the string to be searched is a matrix, it will return a matrix
  2. If the sequence to be searched is a matrix, locate and replace is executed with each matrix element
  3. If both find and replace are matrices, and replace has less elements of what you find, an empty string will be used as replace
  4. If locate is an array and replace is a string, the sequence of replacement will be used for each locate value

Putting theory into practice

Citation used

Save to database with line breaks from :

str_replace("\n",'<brSalve no banco de dados com as quebras de linhas do <textarea>:

str_replace("\n",'<br />', addslashes(htmlspecialchars($_POST['valor'] 

// OPTIONAL: addslashes is for ' character conversion and does not give conlflito no BD // OPTIONAL: htmlspecialchars é para não permitir Special characters //To take the value from the dice do so:

str_replace('<br />', "\n", $valor); />', addslashes(htmlspecialchars($_POST['valor']

// OPTIONAL: addslashes is for ' character conversion and does not give conlflito no BD // OPTIONAL: htmlspecialchars é para não permitir Special characters //To take the value from the dice do so:

str_replace('<br />', "\n", $valor);

Remarks:

PHP offers you an extensive range of opportunities to solve a problem in different ways, look for one that fits better in your programming style.

Doubts, the disposition.

1

The problems boil down to the characters &#13;&#10;

So you don’t need as much code to fix this little detail.

3 Places are enough:

$obs = addslashes(filter_input(INPUT_POST, 'obs', FILTER_SANITIZE_SPECIAL_CHARS));
$obs = str_replace("&#13;",' ', $obs );
$obs = str_replace("&#10;",' ', $obs );
$obs = preg_replace(array("/\t/", "/\s{2,}/", "/\n/"), array("", " ", " "), $obs);

0

This happens because when you skip a line it inserts a \n\r, which are two characters, so when you go to pick it up, it has more than 255 characters.

The \n is a character that breaks the line, and the \r is a character that tells him to return the cursor to the beginning of the line

Copy text from the textarea after giving a enter, and paste it into Notepad++, activating the option to show all characters, and it will show this:

inserir a descrição da imagem aqui

I’m not very good at PHP, but I think you can resolve using the following line to remove the occurrence of these characters:

$text = str_replace("\r\n",'', $text);

  • the same mistake I think, but your solution did not work

  • not error code, error occurs in update I try in mysql.

Browser other questions tagged

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