如何允许只有空格和alpha在名称字段PHP使用regex


how to allow only spaces and alpha in name fields php using regex

下面是我的正则表达式,只允许在名字和姓氏中使用单引号和连字符的字母和空格

 $no_digit = array('firstname'=>$_POST['firstname'],'lastname'=>$_POST['lastname'],'city'=>$_POST['city']
                               ,'country'=>$_POST['country'],'state'=>$_POST['state']);
            foreach($no_digit as $index => $text){
                echo '<br>regex = ' . preg_match("#[a-zA-Z - ']#",$text);
                    $error_flag = true;
                    $error[$index] = "Wrong Character Found";

            }

我不知道它是不是在做我想要的,请有人告诉我应该怎么做,或者我错过了什么,或者做错了什么…

if(preg_match("~[^a-zA-Z'-']~",$text)){
    $error_flag = true;
    $error[$index] = "Wrong Character Found";
}