Regex to match ALL-CAPS words of a certain length

前端 未结 2 443
时光取名叫无心
时光取名叫无心 2021-01-18 11:26

I have a function which fixes capitalization for those naughty users that insist on making everything UPPERCASE!

I want my function to only be called when a string c

2条回答
  •  醉梦人生
    2021-01-18 12:30

    if (preg_match('/\b\p{L}*\p{Lu}{3}\p{L}*\b/u', $str)) {
        // Naughty user!
    }
    

    will match any word that contains at least three uppercase letters. It doesn't matter whether the word starts with an uppercase or lowercase letter, so it would match, for example iTUNES or StackOVERflow as complete words.

    If you want to restrict yourself to words that consist entirely of uppercase characters, three or more, then use

    if (preg_match('/\b\p{Lu}{3,}\b/u', $str)) {
        // Naughty user!
    }
    

提交回复
热议问题