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

22,208 responses to “Limit the number of newlines in a string”

  1. kontol babi Avatar

    Howdy! This is kind of off topic but I need some advice from an established blog.

    Is it hard to set up your own blog? I’m not
    very techincal but I can figure things out pretty fast.
    I’m thinking about creating my own but I’m not sure
    where to begin. Do you have any ideas or suggestions?
    With thanks

  2. Watch this video Avatar

    I used to be recommended this website via my
    cousin. I am now not certain whether this publish
    is written by him as no one else realize such designated approximately my problem.
    You are incredible! Thank you!

  3. article Avatar

    I didn’t see it coming at all, but the day I stepped into the backend of the sports world, everything
    I thought I knew about modern sports completely collapsed.

    I always believed sports were about competition, but when I saw
    how the entire industry had turned into this multi-layered financial machine,
    I honestly felt overwhelmed.

    What hit me hardest was realizing that cybercriminals weren’t going after
    “IT servers” anymore. They were attacking everything — live match stats.

    One breach, and millions could be lost instantly.

    And the scariest part?
    I saw it with my own eyes.

    One day, during a routine check, someone mentioned how a ransomware attack almost froze an entire club.

    That was the moment I understood how thin the line really
    is between order and total digital disaster.

    But the real shock came when I looked deeper into
    betting platforms.
    I swear, I always thought they were just websites.
    But no.
    They are insane real-time data engines. One tiny manipulation can shift odds globally or make
    entire markets react like they’re on fire.

    When I learned how seriously regulated environments handle security — especially the kind you see described in official breakdowns
    — it opened my eyes. If anyone wants to understand how this level of protection actually works, they can always look it up directly from the
    source. The full explanations are there, and they’re honestly worth reading.

    Inside the clubs, it was even more surreal. I saw databases full of tactical plans — things
    that could ruin a season if leaked.

    And yet the biggest weakness wasn’t the firewalls.
    It was the people.
    One emotional click, one careless moment, and the whole system can fall apart.

    I watched entire staff units go through security training because phishing had become a daily threat.

    Compliance teams were everywhere too — checking access, reviewing logs, forcing documentation.
    I used to think compliance was boring, but now I understand that without
    it, the entire digital side of sports would melt into chaos.

    And the future… that’s another story.

    AI detection, automated response systems, cloud architectures — it
    felt like watching the digital version of an arms race.
    But the truth I walked away with was simple:

    Cybersecurity isn’t some extra cost. It’s the only thing holding this whole world together.

    And honestly, after everything I saw, I can say the system I was reviewing really did
    an exceptional job. They handled their mission far
    better than I expected.

    If anyone wants to dig deeper or understand how these structures really work,
    I’d definitely suggest getting more information directly from the source.
    It’s all publicly explained — and believe me, once you read it, you’ll never look at
    sports or betting the same way again.

  4. qris108 Avatar

    QRIS108 merupakan situs game online alternatif resmi dari
    website QR108 yang menyediakan game zeus dari Pragmatic Play
    dan PG Soft, daftar lalu login sekarang juga

  5. klevomedia.com Avatar

    I know this if off topic but I’m looking into starting my own blog and was wondering what all is needed to get set up?
    I’m assuming having a blog like yours would cost a pretty
    penny? I’m not very web smart so I’m not 100% positive. Any
    tips or advice would be greatly appreciated. Kudos

  6. 비아그라 구매 Avatar

    Greetings! I know this is kinda off topic but I was wondering which blog
    platform are you using for this website? I’m getting
    fed up of WordPress because I’ve had issues with hackers and
    I’m looking at options for another platform. I would be fantastic
    if you could point me in the direction of a good
    platform.

  7. egypt visa on arrival Avatar

    I believe this is among the such a lot significant information for me.
    And i’m satisfied reading your article. But should remark on some normal
    issues, The site style is great, the articles is really great : D.
    Just right task, cheers

  8. gt108 daftar Avatar

    GT108 adalah online store produk herbal berkualitas dengan akses resmi, menyediakan informasi, katalog pilihan,
    serta layanan daftar dan login yang aman dan mudah diakses

Leave a Reply

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