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 6365281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:16:26+00:00 2026-05-25T00:16:26+00:00

I am trying to clean up user submitted comments in PHP using regex but

  • 0

I am trying to clean up user submitted comments in PHP using regex but have become rather stuck and confused!

Is it possible using regex to:

  1. Remove punctuation repeated more than twice so that:

    • OMG it was AWESOME!!!! becomes OMG it was AWESOME!!
    • !!!!!!!!!!.........------ becomes !!..--
    • !?!?!? becomes !?
  2. Remove duplicate words of phrases (for example a user has copied and pasted a message) so:

    • spamspamspamspam becomes spam
    • I love copy and paste. I love copy and paste. I love copy and paste. becomes I love copy and paste.
  3. Remove collections of letters and spaces longer than say 10 letters in caps:

    • I LOVE CAPITALS THEY ARE SO AWESOME becomes I love capitals they are so awesome
    • GOOD that sounds stays the same
  4. Any suggestions you have?

This is for a student system (hence the urge to at least try and tidy up what they post), although I do not wish to go as far as filtering it or blocking their messages, just “correct” it with some regex.

Thanks for your time,


Edit:

If it isn’t possible using regex (or regex mised with other PHP) how would you do it?

  • 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-25T00:16:26+00:00Added an answer on May 25, 2026 at 12:16 am

    1:

    // same punctuation repeated more than 2 times
    preg_replace('#([?!.-])\1{2,}#', '$1$1', $string);
    
    // sequence of different punctuations repeated more than one time
    preg_replace('#([?!.-][?!.-]+?)\1+#', '$1', $string);
    

    2:

    // any sequence of characters repeated more than one time
    preg_replace('#(.{2,}?)\1+#', '$1', $string);
    

    3:

    // sequence of uppercase letters and spaces
    function tolower_cb($match) {
            return strtolower($match[0]);
    }
    preg_replace_callback('#([A-Z ]{10,})#', 'tolower_cb', $string);
    

    Try it here: http://codepad.org/iQsZ2vJ0

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

Sidebar

Related Questions

I am trying to request.user for a form's clean method, but how can I
I am using htmlpurifier to clean up user content. I am trying to remove
I am trying to keep my code as secure/clean as possible. If I have
I'm trying to clean up form input using the following Perl transliteration: sub ValidateInput
I am trying to access the XML http://webservices.ns.nl/ns-api-stations using ASIHTTPRequest. But what I am
I am trying to store user input in a variable and clean that variable
I'm trying to clean some preferences files when a user uninstall my extension from
I'm trying to clean up my URLs using mod_rewrite in my .htaccess files. Let's
I am not a database guy, but am trying to clean up another database.
I was trying to clean up some accessability stuff in my code, and inadvertently

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.