Search code examples
phppreg-replacestrip

preg_replace / strip only two uppercase letters when together


I have a unique thing here when in a sentence I have two uppercase letters together. Like this:

The AA batteries. The CA power. The WV chronicles.

etc.

How do you I strip just those uppercase letters? Thanks!!


Solution

  • Here ya go

    preg_match_all("/\b([A-Z]{2})\b/",$string,$matches);
    /*
    --matches
    [0] => The AA batteries. The CA power. The WV chronicles.
    [1] => AA
    [2] => CA
    [3] => WV
    
    */
    

    https://regex101.com/r/rT3jK7/1

    EDIT - Edited to use preg_match_all instead of preg_match