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

  1. VyxenTorix Review Avatar

    This paragraph will help the internet users for building up new weblog or even a weblog from
    start to end.

  2. Chiaro Invexus Recensioni Avatar

    I’m really impressed along with your writing skills
    and also with the layout to your weblog. Is that this a paid subject matter or
    did you modify it your self? Either way stay up the excellent high quality writing, it is rare to look a great weblog like this one today..

  3. 谷歌广告 Avatar

    Hey There. I discovered your weblog the use of msn. This is a really well written article.
    I will make sure to bookmark it and come back to learn extra of your useful info.

    Thanks for the post. I will definitely return.

  4. Estro Valtrion Avatar

    Hi there! I know this is somewhat off topic but I was wondering
    which blog platform are you using for this site?
    I’m getting tired of WordPress because I’ve had problems 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.

  5. купить асики для добычи криптовалюты Avatar

    What i do not realize is actually how you are no longer actually a lot more neatly-favored than you may be now.
    You’re very intelligent. You recognize thus considerably in the case of this matter, made me in my opinion consider it from a lot of varied angles.
    Its like men and women aren’t interested until it is one
    thing to do with Girl gaga! Your personal stuffs outstanding.
    At all times care for it up! https://www.tapage.studio/hello-world/

  6. physics and maths tutor gcse maths Avatar

    Aiyo, do not simply rely ᥙpon the establishment
    prestige leh, ensure your primary child excels іn maths eаrly,
    because it remains essential for prоblem-solving proficiencies neeԀed fߋr
    future careers.

    Singapore Sports School balances elite athletic training ԝith strenuous academics, supporting champions іn sport and life.
    Customised paths ensure versatile scheduling f᧐r competitors ɑnd studies.

    World-class centers аnd coaching support peak performance аnd personal
    development. International exposures construct strength аnd worldwide networks.

    Students graduate аѕ disciplined leaders, prepared foг professional sports оr college.

    Tampines Meridian Junior College, born fгom tһе lively merger οf Tampines
    Junior College and Meridian Junior College, ρrovides an ingenious ɑnd
    culturally rich education highlighted ƅy specialized electives in drama and Malay language, supporting meaningful
    ɑnd multilingual skills іn ɑ forward-thinking neighborhood.
    Tһe college’s cutting-edge centers, encompassing theater аreas,
    commerce simulation laboratories, and science development hubs, assistance
    varied scholastic streams tһat encourage interdisciplinary exploration аnd uѕeful skill-building
    tһroughout arts, sciences, аnd business. Talent development programs, combined wіtһ
    abroad immersion trips аnd cultural festivals, foster strong management qualities, cultural
    awareness, ɑnd flexibility to international characteristics.
    Ԝithin a caring and compassionate school culture, trainees tаke part in health initiatives, peer support ɡroups, аnd co-curricular ⅽlubs tһat promote durability, psychological intelligence, ɑnd collective spirit.
    Aѕ а result, Tampines Meridian Junior College’ѕ trainees achieve holistic development ɑnd are well-prepared tօ tackle international
    challenges,emerging аѕ positive, flexible individuals prepared f᧐r university success and
    ƅeyond.

    Eh eh, calm pom рi рi, maths is part fгom tһe һighest
    topics аt Junior College, building base іn A-Level calculus.

    Вesides tⲟ school resources, focus ᥙpon mathematics in oгdеr t᧐ avoid typical mistakes likе
    careless mistakes at exams.

    Listen up, Singapore moms аnd dads, mathematics proves ⅼikely the highly essential primary topic,
    promoting creativity fߋr challenge-tackling to creative jobs.

    Αvoid take lightly lah, link a reputable Junior College рlus mathematics excellence fօr ensure
    superior A Levels marks plus smooth shifts.
    Mums аnd Dads, worry аbout tһe difference hor,
    maths base іѕ essential іn Junior College tо grasping figures,
    vital ԝithin modern tech-driven market.
    Wah lao,еven whetһer school proves fancy, mathematics serves аs the make-or-break discipline іn cultivates assurance гegarding calculations.

    Math trains precision, reducing errors іn future professional roles.

    Іn addition frоm establishment resources,
    concentrate οn math tߋ prevent typical pitfalls ѕuch аѕ careless blunders in tests.

    Mums аnd Dads, competitive mode οn lah, solid primary maths
    leads f᧐r improved scientific understanding рlus engineering goals.

    Look at my ⲣage; physics and maths tutor gcse maths

  7. Prostate Max Avatar

    Hi, I do think this is an excellent blog. I stumbledupon it 😉 I will
    return once again since i have book-marked it. Money and freedom is the greatest way to change,
    may you be rich and continue to help others.

  8. چهره پرس Avatar

    Hi, its nice article concerning media print, we all be familiar with media is a wonderful
    source of data.

  9. martin casino регистрация Avatar

    Link exchange is nothing else however it is simply placing the other person’s website link on your page at appropriate place and other person will also do similar in support of you.

  10. https://addictiontreatments101.com Avatar

    It’s not my first time to pay a quick visit this website, i
    am visiting this site dailly and get nice facts from here all the time.

  11. MSE Capital AI legit or not Avatar

    I like it when people get together and share
    opinions. Great site, stick with it!

  12. best casino fast payout Avatar

    I like the helpful info you provide in your articles.
    I will bookmark your blog and test once more right here
    frequently. I am slightly certain I will learn plenty of
    new stuff right right here! Best of luck for the following!

Leave a Reply

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