Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7076481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:18:12+00:00 2026-05-28T06:18:12+00:00

Input form on my website for comments. I have some silly users creating a

  • 0

Input form on my website for comments. I have some silly users creating a whole lot of blank whitespace by pressing enter [space] enter [space] or lots of enters etc
In PHP, how do I preserve legit single enter or double ‘enter enter’, but remove any other combinations with spaces inbetween.
eg, This is OK:

\n\n

This is NOT:

\n\n\n...
\n\n \n...
\n \n \n...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:18:13+00:00Added an answer on May 28, 2026 at 6:18 am

    Finally got it working and tested. I decided to break the process in two passes of replaces to keep the regex code simple (you can combine both if you desire).

    First one (/\n | {1,} | \n/) will look for any random combination of spaces and \n characters, leaving the solo \n.

    Second one ('/(?<=\n{2})\n*/') makes use of look behind feature to match any group of \ncharacters that follows any ‘\n\n’ (double new line sequence).

    Testing:

    $string ="
    Loren Ipsum Dolor Sit Amet\n
    Loren Ipsum Dolor Sit Amet\n
    \n
    Loren Ipsum Dolor Sit Amet\n
    \n
    \n
    Loren Ipsum Dolor Sit Amet\n
    \n
    \n
    \n
    .\n
    \n
    ..\n
    .....\n
    .\n
    ...\n
    Loren Ipsum Dolor Sit Amet";
    
    $string = preg_replace('/\n | {1,} | \n/', '', $string);
    $string = preg_replace('/(?<=\n{2})\n*/', '', $string);
    echo $string;
    

    Output:

    Loren Ipsum Dolor Sit Amet\n
    Loren Ipsum Dolor Sit Amet\n
    \n
    Loren Ipsum Dolor Sit Amet\n
    \n
    Loren Ipsum Dolor Sit Amet\n
    \n
    Loren Ipsum Dolor Sit Amet
    

    I’m not a regex guru, but I think it already solves the problem decently.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

On a website if I have a form where the user can input some
I have a Pictures section on my website and allow users to enter comments
I have a website form that requires a US phone number input for follow
I have a form where depending on the website's brand one of two input
I have an input form on my website where HTML is allowed and I'm
I have a registration form on my website and need to validate the input
I have a search form on my website that allows users to search for
I have an input form with two textareas allowing a user to type in
I have a complex input form within a PHP based web application. To structure
I have a shipping/billing input form and I'm having trouble styling the input fields

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.