If you do not care what characters, you do not need a regular expression. Use strlen to check the length of a string:
if ((strlen($yourString) <= 40) && (strlen($yourString) >= 1)) { }
This will be much faster than loading the PCRE processor.
Addendum: if your string can contain multibyte characters (e.g. é ), you should use mb_strlen , which takes these characters into account.
source share