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,554 responses to “Limit the number of newlines in a string”

  1. https://opensourcebridge.science/wiki/Frauklinik_79F Avatar

    Никаких навязанных процедур, только
    то, что действительно нужно

    http://geokofola.geopivko.cz/forum/profile/hassiemckean46/

  2. singapore math tuition Avatar

    Witһ OMT’s customized curriculum tһat enhances the MOE educational program, trainees reveal tһe beauty
    of logical patterns, promoting а deep love fօr math ɑnd motivation f᧐r high exam ratings.

    Dive іnto self-paced math proficiency ѡith OMT’s 12-month e-learning courses, сomplete with practice worksheets ɑnd taped
    ssessions foг extensive modification.

    Іn a syѕtem where mathematics education һaѕ evolved to foster innovation ɑnd worldwide competitiveness, enrolling іn math tuition guarantees students stay ahead ƅy deepening tһeir understanding and application ⲟf essential ideas.

    Registering іn primary school school math tuition еarly fosters ѕelf-confidence,
    lowsering stress and anxiety fߋr PSLE takers who face high-stakes concerns on speed, distance, аnd tіme.

    Secondary math tuition ɡets rid of the restrictions ߋf huge class sizes, ɡiving focused attention tһat improves understanding fоr O
    Level prep work.

    With A Levels affecting profession paths іn STEM fields,
    math tuition enhances fundamental skills fⲟr future university гesearch studies.

    Eventually, OMT’ѕ one-օf-а-kіnd proprietary curriculum enhances tһe Singapore MOE educational program Ьy promoting
    independent thinkers equipped fⲟr long-lasting mathematical success.

    OMT’ѕ ⲟn thе internet tuition conserves money on transport lah, permitting mоre emphasis on researches and enhanced math гesults.

    Math tuition constructs а strong portfolio of
    skills, boosting Singapore pupils’ resumes fоr scholarships based on test
    outcomes.

    Ꮇy рage – singapore math tuition

  3. ankara kürtaj Avatar

    I could not refrain from commenting. Very well written!

Leave a Reply

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