How can I remove duplicate characters in a string?

Discussion in 'PHP' started by Airwalk Enterprise, Inc, Jul 12, 2011.

  1. #1
    So I have $comment but they decided to say "aaaaaaaaaaaaaa" or any other string of characters that is repeated more than 3 times.

    How do I remove those words from the string?

    It would be ideal to check if any characters are repeated more than 3 times and replace those characters with a blank space, or something like that

    Any ideas?
     
    Airwalk Enterprise, Inc, Jul 12, 2011 IP
  2. rajmv

    rajmv Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    preg_replace can probably do it, but i have no clue about what reg exp is needed for this. sorry.
     
    rajmv, Jul 12, 2011 IP
  3. Airwalk Enterprise, Inc

    Airwalk Enterprise, Inc Peon

    Messages:
    126
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes I figured as much, but I'm useless with regex.
     
    Airwalk Enterprise, Inc, Jul 12, 2011 IP
  4. oxyzenwebmedia

    oxyzenwebmedia Peon

    Messages:
    18
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    $string = 'aabbccaaaaaddee';
    $new_string = '';
    $starting_char = 0;
    while (strlen($string) > 0 && $starting_char < strlen($string)) {
        $blah = preg_match('/[A-z]{3,}/', $string, $matches);
        $letter = $matches[0][$starting_char];
        $new_string .= $letter;
        $regex = '/' . $letter . '{3,}/';
        $string = preg_replace($regex, $letter, $string);
        $starting_char++;
    }
    echo $new_string;
    PHP:
     
    oxyzenwebmedia, Jul 16, 2011 IP