Error with line break in textarea

1

Well I have a field in the mysql database of type 255.

I made a textarea like this:

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

If I put 255 characters inside it, I can save it without problems. But if I delete a character and skip a line I can not save. Does anybody know how to solve this? to remove line breaks?

I'm capturing it 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 bd &#13;&#10;

    
asked by anonymous 03.04.2017 / 16:19

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 noticed that:

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

I suggest you increase the size in the field in MySQL or narrow the page and, if you need accuracy, control the string size within textarea with JavaScript to limit the amount of information to be allowed before sending.

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

With the change in your question that informs 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 your problem should be solved.

    
03.04.2017 / 17:06
1

How do I solve the problem immediately?

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

For study purposes, understanding the function used

Understanding the str_replace function used

  

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

     

This function works by the following rules:

     
  • If the string to be searched is an array, it   will return an array
  •   
  • If the string to be searched is an array, find and   replace is executed with each array element
  •   
  • If both find and replace are arrays, and replace has less   elements to find, an empty string will be used as   replace
  •   
  • If locate is an array and replace is a string, the string of   substitution will be used for each value to find
  •   

    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 in BD // OPTIONAL: htmlspecialchars is not allowed   special characters // To retrieve the value from the database   do so:

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

    // OPTIONAL: addslashes is for character conversion and does not give   conlflito in BD // OPTIONAL: htmlspecialchars is not allowed   special characters // To retrieve the value from the database   do so:

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

    Observations:

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

    Questions, the willingness.

        
    03.04.2017 / 16:28
    1

    Problems are limited to characters &#13;&#10;

    So you do not need so much code to heal this little detail.

    Just 3 replaces 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);
    
        
    03.04.2017 / 18:57
    0

    This happens because when you skip a line it inserts a \n\r , which is two characters, then when you go to get it, it has more than 255 characters.

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

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

    I'mnotverygoodatPHP,butIthinkyoucansolveusingthefollowinglinetoremovetheoccurrenceofthesecharacters:

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

        
    03.04.2017 / 16:27