正则表达式为人们命名PHP

I am using this regex to make sure that names can only contain letters, apostrophe or hyphen.

Validator::extend('people_names', function($attribute, $value, $parameters, $validator) {
        return preg_match("^[a-zA-Z-'\s]+$^", $value);

When I use this tool to check if the regex is correct, it shows its correct, but in my form when I test an input starting with digits or any other characters like 123name or #$#$name it passes while it should not. An input with letters then digits like name123 is rejected.

The PHP preg_ functions need to start with /^ and end with $/.

Try this:

preg_match("/^[a-zA-Z-'\s]+$/", $value);

In your regex ^[a-zA-Z-'\s]+$^, you are only checking if the string ends with a letter by using the $ at the end, but the ^ at the beginning is used here asthe regex delimiter, since it's also at the end.

I'd recommend using another string delimiter, such as # or / :

preg_match("#^[a-zA-Z-'\s]+$#", $value) should work.