Limit the number of newlines in a string
A very short blog post this time. I often use the following short function to limit the number of breaks/newlines in comments that people can submit on a news article or other user input. You can easily limit the number of characters in a comment by using the PHP strlen function, but the comment can still use a lot of vertical space on your website if the user uses 5 breaks in his comment. See the function below to prevent this type of comments:
/**
* limitBreaks, prevents texts with a lot of enters/breaks after each other
*
* @param string $sText
* @param int $iAmount default 2, numbers of newlines that may occur after eachother
* @return string, cleaned up string with limited number of newlines
*/
function limitBreaks($sText, $iAmount=2)
{
return preg_replace("/[\r\n]{".($iAmount+1).",}\t*[\r\n]*/", str_repeat(PHP_EOL, $iAmount), $sText);
}
As you can see a simple but effective function.
https://t.me/bs_1Win/645
Georgerah
27 Oct 25 at 10:44 pm
https://t.me/s/bs_1Win/1080
Georgerah
27 Oct 25 at 10:58 pm
https://t.me/bs_1Win/868
Georgerah
27 Oct 25 at 11:12 pm
https://t.me/s/bs_1Win/946
Georgerah
27 Oct 25 at 11:37 pm
https://t.me/bs_1Win/595
Georgerah
27 Oct 25 at 11:53 pm
https://t.me/s/bs_1Win/1065
Georgerah
28 Oct 25 at 12:08 am
https://t.me/bs_1Win/771
Georgerah
28 Oct 25 at 12:21 am
https://t.me/bs_1Win/951
Georgerah
28 Oct 25 at 12:46 am
https://t.me/s/bs_1Win/992
Georgerah
28 Oct 25 at 1:04 am
https://t.me/bs_1Win/607
Georgerah
28 Oct 25 at 1:17 am
https://t.me/bs_1Win/689
Georgerah
28 Oct 25 at 1:31 am
https://t.me/bs_1Win/947
Georgerah
28 Oct 25 at 1:56 am
https://t.me/s/bs_1Win/1234
Georgerah
28 Oct 25 at 2:13 am
https://t.me/s/bs_1Win/606
Georgerah
28 Oct 25 at 2:27 am
https://t.me/s/bs_1Win/1292
Georgerah
28 Oct 25 at 2:41 am
https://t.me/bs_1Win/813
Georgerah
28 Oct 25 at 3:05 am
https://t.me/bs_1Win/524
Georgerah
28 Oct 25 at 3:22 am
https://t.me/s/bs_1Win/656
Georgerah
28 Oct 25 at 3:37 am