Search code examples
phpregexencodingutf-8preg-match

Minimum one upper character in string


How can I check if string contains a minimum one uppercase character in encoding UTF-8? I checked this with preg_match

preg_match('/[A-Z]/', $var)

but this code doesn't work with all characters for example Ó, Ł.

How can I fix it?


Solution

  • A-Z is looking between the ascii ranges. The characters you are displaying are outside of that range. Use \p{Lu} and use the unicode modifier u.

    preg_match('/\p{Lu}/u', $var)
    

    Demo: https://regex101.com/r/WZaOCD/1/

    For more unicode options please see http://php.net/manual/en/regexp.reference.unicode.php.