You should not use htmlentities
before checking your strings, you may use it after.
If you apply $name = htmlentities($name, ENT_QUOTES, "UTF-8");
to some'name
, it will turn into some'name
.
Also, to shorten the pattern, use
preg_match("/^[A-Z'-]{2,50}$/i", $name)
The /i
case insensitive modifier will make the whole pattern case insensitive, and [A-Z]
will match all upper- and lowercase ASCII letters.