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.
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
should work.
Some preliminary testing shows that
will output
This appears to pass all your test cases.
Re: Gordon
Your string:
doesn’t repeat anything but the first part more than two times. It seems to require:
before it simplifies to
(Which implies that
preg_replaceisn’t Unicode-aware – oh well)you can also adjust it to repeat only once:
in which case it becomes:
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:
(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.)