0

Possible Duplicate:
PHP - Need to remove duplicate characters within a String but would like to include exceptions

I have a string of text below in php.

$string = "@@@@@@ CHECK ME OUT! @@@@@@@@";

or

$string = "//////// CHECK ME OUT! ////////";

The will pretty much will use any character word or key to stand out from the rest when they post. My question is, is there a function or something I can use to prevent people from spamming a key or character.

So if what ever character or key is used more then three times it will remove it. So it will look like this below.

$string = "CHECK ME OUT!";

Thanks

Community
  • 1
  • 1
Tabatha M
  • 171
  • 2
  • 6
  • 13

3 Answers3

3

Sure, just use a regular expression to match a repeated character:

$string = "//////// CHECK ME OUT! ////////";
echo preg_replace('/(.)\1{3,}/', '', $string);
Community
  • 1
  • 1
phihag
  • 278,196
  • 72
  • 453
  • 469
1

You can supply a charlist in trim.

$string = "//////// CHECK ME OUT! ////////";
echo trim($string, "/\$@");

This will trim (from front and end of string) any character in the list. No strings should being or end with /\%@ so that should be a pretty safe thing to do.

sachleen
  • 30,730
  • 8
  • 78
  • 73
0
$string = preg_replace('/(.)\1\1+/', '', $string);
Michael Robinson
  • 29,278
  • 12
  • 104
  • 130