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

16,951 responses to “Limit the number of newlines in a string”

  1. answer Avatar

    I’ve been reading Paybis for a while now, especially after going through financial
    trouble, and I’m still not fully convinced whether it deserves all the attention it gets.

    Still, it’s clearly a noticeable name in the digital asset
    market, particularly for people in Germany
    who are trying to get back on track. From what I understand, Paybis
    presents itself as a large-scale cryptocurrency service that
    supports regular bank transactions, something many platforms either limit or complicate.

    What initially caught my eye is how Paybis seems to link traditional German banking methods with the crypto world.

    Many exchanges focus only on crypto-to-crypto trades, while Paybis allows users to buy crypto using various fiat options.
    I’m not saying the process is perfect, but it does seem
    aimed at beginners rather than just advanced traders.

    Another aspect worth noting is the selection of supported assets.

    Paybis doesn’t restrict itself to only the major tokens.
    Instead, it offers a broader token selection, which might attract
    users who are trying new projects. Still, things like update speed are worth
    checking before making decisions.

    Security and compliance also come up often around Paybis.

    The platform highlights KYC checks, which can feel professional for users in Germany, though others might see it as time-consuming.

    I’m still undecided, but it does suggest Paybis tries to operate as a regulated
    marketplace.

    When it comes to fees, reviews seem varied. Some say Paybis is transparent about costs, while others note that pricing
    can vary by payment method. This isn’t unusual in the crypto industry,
    but it means users should read the details before
    moving money.

    Overall, I wouldn’t call Paybis perfect, but it does seem like a platform worth checking out, especially for someone
    in Germany trying to find accessible financial tools.
    I’m still forming my opinion, but it seems relevant enough to justify further
    research.

Leave a Reply

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