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

5,356 responses to “Limit the number of newlines in a string”

  1. big dick Avatar

    Hi! Do you know if they make any plugins to assist with Search Engine Optimization? I’m trying to get my
    blog to rank for some targeted keywords but I’m not seeing
    very good results. If you know of any please share. Kudos!

  2. make bomb Avatar

    With havin so much content do you ever run into any problems of plagorism or copyright infringement?

    My website has a lot of unique content I’ve either written myself or outsourced but it
    seems a lot of it is popping it up all over the internet without my authorization. Do you know any ways to
    help protect against content from being stolen? I’d definitely appreciate it.

  3. https://addictiontreatments101.com Avatar

    Admiring the time and effort you put into your site and in depth information you provide.

    It’s nice to come across a blog every once in a while that isn’t
    the same unwanted rehashed information. Excellent read!
    I’ve saved your site and I’m adding your RSS feeds to my Google account.

  4. bandar teslatoto Avatar

    TESLATOTO menyediakan hasil langsung Toto Macau 4D tercepat dengan result akurat dan payout
    100%. Nikmati informasi hasil terupdate, paito warna, rumusan akurat, serta bocoran angka valid setiap hari.
    Pantau keluaran terbaru dan maksimalkan kemenangan Anda hanya di TeslaToto.

  5. Lacy Avatar

    Amazing! Its truⅼy amazing paragraph, I haᴠe got much cleaг idea regarding fгom thіs paragraph.

    Feel free tⲟ visit mʏ web site :: math and science tuition singapore
    (Lacy)

  6. mad казино лицензия Avatar

    This design is incredible! You most certainly know how to keep a reader entertained.
    Between your wit and your videos, I was almost moved to start
    my own blog (well, almost…HaHa!) Great job. I really loved
    what you had to say, and more than that, how you presented it.
    Too cool!

Leave a Reply

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