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

42,263 responses to “Limit the number of newlines in a string”

  1. Jeremy Owen Avatar

    For any individual evaluating dollars for cars Barrie recommendations, verify you get distinctive costs. I got the gold standard price and quickest pickup using https://maps.google.com/maps?ll=44.380207,-79.705233&z=16&t=h&hl=en&gl=US&mapclient=embed&cid=3848532738971167870 . Super gentle strategy for scrap motor vehicle removal Barrie.

  2. Eric Carson Avatar

    Downsizing to a condo near downtown Roanoke and need careful furniture disassembly/reassembly. Would love feedback on crews you trust— Roanoke Mover’s Services seems promising.

  3. Black hat SEO Avatar

    What’s up to every body, it’s my first pay a quick visit of this website; this website consists of
    awesome and truly excellent data in support of visitors.

  4. Ophelia Lawrence Avatar

    Tripped breaker won’t hold— Macquarie Park NSW 2113, Australia electrician traced a nail through a cable.

  5. Juan Clayton Avatar

    Love how the movers protected door frames and floors during my Conyers townhouse move. Booked them via Best Conyers movers and would do it again.

  6. bokep Avatar

    Someone necessarily lend a hand to make critically articles I would state.

    That is the very first time I frequented your web page
    and up to now? I surprised with the analysis
    you made to create this particular submit amazing. Excellent activity!

  7. Elva Carter Avatar

    I’m always nervous about moving, but hiring a Camden moving company sounds like the right choice. Camden Mover’s

  8. Brent Knight Avatar

    We did a weekend move within Cumming and Cumming moving companies made it seamless. Loved their labeling system for each room.

  9. Mattie Lopez Avatar

    Loved the reminder to set a practical budget diversity. When I tackled bathing room remodeling Peoria IL, swap orders introduced up swift until eventually I added in a team that scoped the whole lot effectively deck builders peoria il

  10. Myra Terry Avatar

    Goede tips over verantwoord spelen. Ik zet altijd limieten en doe eerst de demoversie van slots voor ik echt inzet legale Nederlandse casino’s

  11. Gilbert Paul Avatar

    Thinking approximately a fiberglass pool contractor Walla Walla citizens put forward? Fiberglass shells can installation speedier and require less upkeep than many be expecting pool installation walla walla

  12. Juan Watkins Avatar

    I recently moved to Puyallup, and I was shocked by how many pests I encountered in my new home! After doing some research on pest control options, I found that local services are crucial for effective solutions nearby exterminator services

Leave a Reply

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