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.

Comments

26,463 responses to “Limit the number of newlines in a string”

  1. Susie Day Avatar

    Thanks for the helpful article. More like this at Millsboro roof wash .

  2. Marc Lucas Avatar

    Danke für die Checkliste zur Küche und Badreinigung. Für Profis empfehle ich Umzugsreinigung Preise Zürich .

  3. Olive Nichols Avatar

    Mọi thông tin đều rõ ràng, link https://nohu.voto tiện cho người mới.

  4. Derek Yates Avatar

    Great roundup on riskless tree removal practices. In El Monte, permits and proximity to pressure strains can complicate DIY jobs fast Get More Information

  5. Nathan Little Avatar

    Great insights! Discover more at personal injury lawyers .

  6. Lou Richardson Avatar

    Just got my car key replaced by a professional locksmith from Orange County – so grateful for locksmith in orange county !

  7. Dora Simpson Avatar

    Has anyone tried eco-friendly paver stones? I’m interested in sustainable options for my landscaping project. Would love to hear your thoughts and also check out hardscape contractor services for options!

  8. Thomastraro Avatar

    Всегда пользуюсь промокодами, и Мелбет не исключение. Ввел код при регистрации — получил бонус без лишних вопросов. Главное — выполнять условия отыгрыша, и тогда все будет нормально. В целом, система бонусов у них продумана. http://pytalovo.4admins.ru/viewtopic.php?f=26&t=5660

  9. Jayden Shelton Avatar

    Without an growth cylinder, a water boiler was installed on a sealed program. After reading https://www.divephotoguide.com/user/donatampxy/ , I flagged it.

  10. Eva Medina Avatar

    If you’re noticing higher humidity indoors in Southampton, try: hvac southampton

  11. Craig Fuller Avatar

    This migration checklist saved us. search engine optimization services handled redirects flawlessly and protected our rankings.

  12. Lena Norman Avatar

    ”Empathy flourishes when narratives resonate deeply inspiring others ultimately igniting change throughout society at large… ” semi truck accident lawyer

  13. Addie Nichols Avatar

    Good read — residents can request a State Farm auto quote from State farm agent online or by phone.

Leave a Reply

Your email address will not be published. Required fields are marked *