function validateName($name){ $pattern = "/^[a-zA-Z' -]+$/"; if(preg_match($pattern,$name)){ return true; }else{ return false; } }
$fullName = "John Doe"; if(filter_var($fullName, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z' -]+$/"))) === false){ echo("Invalid name"); }In this example, we are using the PHP filter_var function to validate the input string as a name. It uses the FILTER_VALIDATE_REGEXP option to determine if the input string matches a regular expression that defines a valid name pattern. If the input fails the validation, the code echoes "Invalid name".