Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if a non-English string is in upper case?

Tags:

regex

php

pcre

I'm using the following code to check for a string where all the characters are upper-case letters:

        if (preg_match('/^[\p{Lu}]+$/', $word)) {

This works great for English, but fails to detect letters with accents, Russian letters, etc. Is \p{Lu} supposed to work for all languages? Is there a better approach?

like image 576
Boris Burtin Avatar asked Apr 21 '11 21:04

Boris Burtin


People also ask

How do you check if a string has an uppercase letter?

Traverse the string character by character from start to end. Check the ASCII value of each character for the following conditions: If the ASCII value lies in the range of [65, 90], then it is an uppercase letter. If the ASCII value lies in the range of [97, 122], then it is a lowercase letter.

How do you check if all letters in a string are capital letters Python?

In Python, isupper() is a built-in method used for string handling. This method returns True if all characters in the string are uppercase, otherwise, returns “False”.

How do you check if a string is all lowercase?

The islower() method returns True if all alphabets in a string are lowercase alphabets. If the string contains at least one uppercase alphabet, it returns False.


1 Answers

A special option is the /u which turns on the Unicode matching mode, instead of the default 8-bit matching mode. You should specify /u for regular expressions that use \x{FFFF}, \X or \p{L} to match Unicode characters, graphemes, properties or scripts. PHP will interpret '/regex/u' as a UTF-8 string rather than as an ASCII string.

http://www.regular-expressions.info/php.html --

like image 102
sdolgy Avatar answered Oct 19 '22 03:10

sdolgy