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

32,064 responses to “Limit the number of newlines in a string”

  1. Gabriel Ball Avatar

    I have actually been trying to find the very best Gutters company near me and kept seeing multiple exceptional evaluations about A+ Gutters for World Golf Village gutter installation

  2. Ophelia Francis Avatar

    My Middlefield roof is 20+ years vintage. It’s time. Starting with an inspection from roofer near me .

  3. Norman Goodwin Avatar

    Thanks for the helpful article. More like this at ARD Waterproofing Services .

  4. Hilda Scott Avatar

    Classic truck Irving to Phoenix went enclosed; found a soft tie carrier on long distance car shipping Irving with great feedback.

  5. Mabel Warren Avatar

    Utility potholing is definitely an unsung hero when it comes to city planning here in Sacramento—learn why at Sacramento hydro excavation !

  6. Phoebe Kelley Avatar

    Thanks for demystifying roofing estimates. best roofing company near me awarded a detailed, obvious quote.

  7. Nettie Obrien Avatar

    Gran artículo, me hace pensar en revisar todas las entradas a mi hogar y asegurarme de que estén seguras! cerrajero en Barcelona 24 horas

  8. Georgie Underwood Avatar

    I learned to request soft straps for my rims. Got a careful team from emergency towing .

  9. Lulu Silva Avatar

    Having clear maps showing existing utilities can make such a difference; thanks for driving home that point!! # # anykeyword## Fresno vacuum excavation

  10. Rhoda Goodwin Avatar

    The most top rated moving company unquestionably goes to Jaguar Moving, the most effective moving companies in Jacksonville fl in St Augustine. moving companies jacksonville fl

  11. Betty Doyle Avatar

    Summer canopy thinning more advantageous our garden’s daylight; we booked in Streetsboro as a result of tree removal .

  12. Randy Cook Avatar

    Thanks for sharing. For Arizona claim support, Best Public Adjuster in Arizona has experienced adjusters.

  13. Cory Klein Avatar

    I finally sleep better after EMDR reduced the startle response; sleep hygiene tips at EMDR therapy helped too.

  14. Mark Snyder Avatar

    I not ever found out how a good deal litter affects pressure levels. Living in St Charles MO, I’m purchasing for a speedy approach to transparent out old stuff, so I’m checking out junk removal for a full junk elimination carrier.

Leave a Reply

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