There is a PCRE/POSIX regular expression character class that matches any printable characters (reference). When used with the u
modifier, it will match any UTF-8 printable characters:
[:print:] This matches the same characters as [:graph:] plus space characters that are not controls, that is, characters with the Zs property.
If you are only interested in glyphs and spaces count as no-no, you would use:
[:graph:] This matches characters that have glyphs that mark the page when printed. In Unicode property terms, it matches all characters with the L, M, N, P, S, or Cf properties
Then, your regular expression would look like:
preg_match('~^[[:print:]]+$~u', $string)
And a possible function would look like:
function is_utf8_printable($string): bool {
return (bool) preg_match('~^[[:print:]]+$~u', $string);
}
This will tell you whether all characters, from the ^
beginning to the $
end of the string, match the :print:
character class. Please see here for several test string iterations.