Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Pixelairport's avatar

Textarea validation problem with max, because of \r\n

After a lot of time, I found out that max validation makes problems, when I have an textarea. For example I write this:

Hello
World

This should be 10 chars. But Laravel says its 11. The Problem is that i have set maxlength to the textarea field to 10. And also the validation for max is 10. I type in the whole text with 10 chars and get an error. Because laravel seems to add for each break an \r\n (with an empty space at the end. I mean "\r\n "). I think this could be the problem. But what could I do? I found a lot for replace stuff at google but this all does not help. The \r\n is still there.

0 likes
5 replies
bugsysha's avatar

You can either use regex to do the validation or change that text area to input and ask for a comma delimiter between words.

Pixelairport's avatar

I output the text with dd($mytext) and see the \r\n, but i could nocht replace it. Tried it with str_replace. The \r\n is also orange, and the normal text is green. I just dont get it replaced.

bugsysha's avatar
bugsysha
Best Answer
Level 61

Try

str_replace(PHP_EOL, '', $text);
Pixelairport's avatar

Thank you... that helped me a lot. I have to check some other things... because you said PHP_EOL i had a new thing to search for, so I just found a method i did not knew. strtr(). I everytime used str_replace(). With strtr I can also replace \r\n... I check this and also a PHP_EOL version... thx

Please or to participate in this conversation.