Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sanitize a string with non-alphanum repetition

Tags:

regex

php

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.

like image 848
Toto Avatar asked Mar 28 '10 10:03

Toto


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

like image 92
Zarel Avatar answered Oct 23 '22 05:10

Zarel


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"

like image 45
alex Avatar answered Oct 23 '22 04:10

alex