Question

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 contains an uppercase word of 3 or more uppercase letters.

Can this be done with a regex?

examples: for example: I = false, DEAL = true, Welcome = false

No correct solution

OTHER TIPS

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!
}
if (preg_match('/[A-Z]{3,}|\b[A-Z]\b/', $str)) {
    // Naughty user!
}

Let's look at that...

[A-Z] // Character class from A-Z
{3,} // 3 or more quantifier
| // Logical or pipe
\b // Word boundary
[A-Z] // Character class from A-Z
\b // Word boundary

That may make it easier to understand :)

This will match if between two word boundaries all capitals and/or if there are 3 uppercase letters in a row. Please clarify if that is what you want.

Update

You may want to decide what triggers a whole word with capitals. For example, this sentence would be considered naughty.

I like apples.

.. because of the I. Perhaps you want to put a quantifier there too, like {2,}. It depends on what the string will contain.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top