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.
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.
– Rômulo Gabriel Rodrigues
the error is that Data Too long for column 'Obs' at Row 1
– Hugo Borges
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)
– Rômulo Gabriel Rodrigues
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– Hugo Borges
with 3 Replaces you solve your problem, see in my answer;
– user60252