Search code examples
phpregexrepeatsanitization

Keep only one instance of a specified character consecutively repeated


I have a string !!test or !!!!! testing.

What I want to be able to do is use preg_replace() and only keep the first ! but keep the rest of the text after that first ! that is not a !

!!test -> !test


Solution

  • $str = preg_replace('/!+/', '!', $str);
    

    Replace multiple !s with a single !.

    http://www.regular-expressions.info/ has some great tutorials and references for learning regular expressions.