I had the same problem. At first I came up with something like
preg_match("/^[a-zA-Z]{1,}([\s-]*[a-zA-Z\s\'-]*)$/", $name))
but then I realized that the UTF-8 characters of countries such as Sweden, China, etc., for example, Õ å, will not be allowed, which is important for my site, since this is an international site, and they do not want to force users to not enter your real name.
Although this might be an easier solution instead of trying to figure out how to resolve names like O'Malley and Brooks-Schneider and Õsmar (do it one :) to catch characters that you don't want them to enter more quickly. For me, it was basically avoiding typing xss JS code. Therefore, I use the following regular expression to filter out all characters that may be harmful.
preg_match("/[ ~!@ #\$%\^&\*\(\)=\+\|\[\]\{\};\\:\",\.\<\>\?\/]+/", $name)
Thus, they can enter any name they want, except for characters that really are not part of any name. Hope this can be helpful.
source share