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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:39:57+00:00 2026-05-14T01:39:57+00:00

I need to sanitize article titles when (creative) users try to attract attention with

  • 0

I need to sanitize article titles when (creative) users try to “attract attention” with some non-alphanum repetition.

Exemples:

  • Buy my product !!!!!!!!!!!!!!!!!!!!!!!!
  • Buy my product !? !? !? !? !? !?
  • Buy my product !!!!!!!!!…….!!!!!!!!
  • Buy my product <———–

Some acceptable solution would be to reduce the repetition of non-alphanum to 2.

So I would get:

  • Buy my product !!
  • Buy my product !? !?
  • Buy my product !!..!!
  • Buy my product <–

This solution did not work that well:

preg_replace('/(\W{2,})(?=\1+)/', '', $title)

Any idea how to do it in PHP with regex?

Other better solution is also welcomed (I cannot strip all the non-alphanum characters as they can make sense).

Edit: the objective is only to avoid most common issues. The other creative cases will be sanitized manually or sanitized with an other regex.

  • 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-14T01:39:57+00:00Added an answer on May 14, 2026 at 1:39 am

    That’s really an inefficient problem to solve with a regex, especially if the repeated expression is arbitrarily large. Practically, it shold be enough to just cap the length of the repeated expression at something like 3 to 5, and it should be a lot easier.

    Something like

    $title = preg_replace('/(\W{1,5})(?=\1+)/', '', $title);
    

    should work.

    Some preliminary testing shows that

    $title = 'Buy my product !!!!!!!!!!!!!!!!!!!!!!!! Buy my product !? !? !? !? !? !? Buy my product !!!!!!!!!.......!!!!!!!! Buy my product <-----------';
    
    $title = preg_replace('/(\W{1,5})(?=\1{2,})/', '', $title);
    
    echo $title;
    

    will output

    Buy my product !! Buy my product !? !? Buy my product !!..!! Buy my product <--
    

    This appears to pass all your test cases.


    Re: Gordon

    Your string:

    ¸·´`·¸·´`·¸·´`·¸ Human ·-> creativity << is endless !¡!¡! ☻☺
    

    doesn’t repeat anything but the first part more than two times. It seems to require:

    $title = preg_replace('/(\W{1,9})(?=\1{2,})/', '', $title);
    

    before it simplifies to

    ¸·´`·¸·´`·¸ Human ·-> creativity << is endless !¡!¡! ☻☺
    

    (Which implies that preg_replace isn’t Unicode-aware – oh well)

    you can also adjust it to repeat only once:

    $title = preg_replace('/(\W{1,9})(?=\1+)/', '', $title);
    

    in which case it becomes:

    ¸·´`·¸ Human ·-> creativity < is endless !¡! ☻☺
    

    If your point is that it’s possible to create lots of “ASCII art” even if it’s required to repeat less than two times, well, that’s outside of the scope of this question. For the purposes of keeping ASCII art to a minimum, I would recommend simply using something like:

    preg_replace('/(\W{5})\W+/', '$1', $title);
    

    (i.e. just cap the number of non-alphanumeric characters that can be displayed in a row. Note that this would need to be adjusted for compatibility with languages with non-Latin alphabets, like Russian.)

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

Sidebar

Related Questions

I need to sanitize some data which will be used in file names. Some
Some special characters inside a XML stream may break it. I need to sanitize
I want to sanitize blog titles with unicode characters in url. I need to
Need some regular expressions help. So far I have my code working to allow
Need some help... I have jasperserver 4.1 installed on my ubuntu. It runs via
Need to set some attributes of button. For example Checked. I guess it is
I want my users to be able to write an article in Markdown, have
I really need to get WordPress to sanitize them and replace them with S,
i'm using sanitize::paranoid on a string but i need to exclude a few special
I have an input. I need to sanitize the input's string everytime the user

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.