Giacomo M
Giacomo M

Reputation: 4711

string validation max 15000 characters not working

I am trying to validate a textarea with max 15000 characters validation rule.

This is the rule I created:

array:1 [▼
    "text" => "required|string|max:15000"
]

Somewhere before I concat two inputs in this way:

$request->merge(['text' => $request->input('text') . PHP_EOL . PHP_EOL . $request->input('sign']);

The code does not pass the validation even if the sum of text input and sign input is (according to this website) is 14988.
To calculate it, I just past the textarea value to the website and I checked the characters count.

UPDATE
If I log the length of $request->text input (even if the SAME string written in the log is 15000), I get these values:

strlen(trim($request->text)) => 15937
mb_strlen(trim($request->text)) => 15937

I guess its something with \r, \n or \r\n.

SECOND UPDATE
I created the string from random string websites, and I noticed the string is 937 lines, which is the exact number more than what the string should be.
It seems the PHP count every new line character twice.

Upvotes: 0

Views: 650

Answers (1)

I would suggest that you would rather do a string manipulation by your self before trying to validate, replace new line characters with PHP_EOL this is the built in PHP new line character to handle complexities that comes with dos and Linux new line characters.

when you copy paste from web most of the time <br> tag is converted to \r\n which is recognized as two characters. also most windows text editors outputs the same \r\n newline. that's why the double count.

You can use a regular expression or built in str_replace("\r\n",PHP_EOL,$my_text) to replace new line characters.

Either you can replace \r\n with just \n would work for your character counting purposes but will display new lines inaccurately depending on your application.

Upvotes: 1

Related Questions