PHP - check if a string is a multibyte alphanumeric character

I need to find out if a string contains only one alphanumeric character. The obvious solution would be to check the length and ASCII code ( A-Z, A-Z, 0-9), but the problem is that I work with UTF-8 strings and accented letters, such as á, ř, č, etc.

Is there an easy way to check if a UTF-8 character is alphanumeric (a Latin letter of the alphabet, perhaps an accent or a number)?

+4
source share
1 answer

This is easy to do with a regex :

$count = preg_match_all('/\w/u', $string);
if ($count === 1) {
    echo "One alphanumeric character found";
}

\w "", , . u unicode, .

, [:alnum:] .

0

Source: https://habr.com/ru/post/1545043/


All Articles