If you're not committed to using a regular expression, this function should do the job:
function check_chars($str, $arr)
{
for ($i = 0; $i < strlen($str); $i++) {
if (!in_array($str{$i}, $arr)) {
return false;
}
}
return true;
}
$cap_ltrs = range('A', 'Z');
$sml_ltrs = range('a', 'z');
$nums = range('0', '9');
$symbols = array(' ', '.', '-', "'", '"');
$array = array_merge($cap_ltrs, $sml_ltrs, $nums, $symbols);
if (!check_chars($_POST['first'], $array) || !check_chars($_POST['last'], $array)) {
$result_msg['ERRORNAME'] = "Names can only contain letters, spaces,
periods, quotes or the hyphen character!";
}
echo $result_msg['ERRORNAME'] . '<br />';
It might be more flexible, readable, and maintainable than a regular expression, but it would take up a few more bytes of script storage.