Sanitize a string with non-alphanum repetition

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.

Answers


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.)


I have an answer that's a little bit different

if (preg_match('/^[^\da-z\s_-]$/i', $str)) {

  // auto post, but flag to moderator to inspect title OR
  // instead of auto posting, put in 'waiting to be authorised' by a mod

}

I hope I have that regex correct, but I have not tested it. Basically it should detect when someone has in their title characters that are not 0-9, A-Z (case insensitive), whitespace, underscore and dash. Of course, you can modify this to suit your liking.

It would also be a good idea to inform the end user

"Titles that deliberately try to attract attention without benefiting the product description may be deleted without warning"


Use non-greedy search?

preg_replace('/(\W{2,}?)(?=\1+)/', '', '{{your data}}');

result is

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

Need Your Help

CSS vertically align LI's in middle

html css html-lists

I have the following HTML where I need the LI elements to vertically display in the middle of the 21 pixel high UL area. Here is my HTML...

Can the Java Restlet API Work Be Used With REST PHP APIs?

java php rest networking

Would you be able to use the Java Restlet RESTful API to interface with some provider's API that is written in PHP?