Regex to check if the string contains any letters of any alphabet

There is a trivial task to check if the string contains any letters:

$contains_letters = preg_match('/[A-Za-z]+/', $str);

But what if the $str is a multibyte string and may contain umlauts or Cyrillic letters? That’s what I had recently and I nearly resorted to the ugly solution like this:

$really_contains_letters = (mb_strtoupper($str) == $str && mb_strtolower($str) == $str);

Thankfully it’s not so bad, PHP supports analyzing unicode characters using regex, so the simple:

$really_contains_letters = preg_match('/\pL/', $str);

would do the trick. And I expect it would be faster since it can stop before reading the whole string.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.