This is a simple way to check a UTF-8 string with PHP function preg_match in search for anything that isn't a letter, which includes all UTF-8 letters and not just ASCII. Preg_match performs a regular match on the given input using a pattern. The pattern used tells preg_match to look for letters (\p{L}
) using Unicode (/u
).
The function to accomplish this could look like this:
<?php
function checkUTF8Input($input) {
$pattern = '/^[\p{L} ]+$/u';
preg_match($pattern, $string, $matches);
if (count($matches) > 0) {
return true; // String is OK (only letters)
} else {
return false; // String has non-utf8 letters
}
}
Take a look at preg_match to know more about this function used for regex operations.