I want to validate a string, and it must only contains alphabetic characters. But using below code, it doesn't work. Can anyone tell me why?
if (!eregi("[[:alpha:]]", $_REQUEST["name"]))
echo "Error";
I want to validate a string, and it must only contains alphabetic characters. But using below code, it doesn't work. Can anyone tell me why?
if (!eregi("[[:alpha:]]", $_REQUEST["name"]))
echo "Error";
Because your code is only checking to see if it contains a letter - never mind what any of the rest of it is.
You should look to see if it contains a nonletter - if it does, then it fails.
Weedpacket wrote:You should look to see if it contains a nonletter - if it does, then it fails.
or you have it check if it contains only letters, from the start to the very end
Which (given random input) is more than twice as slow on average....
$search = array_merge(range('a', 'z'), range('A', 'Z'));
if (str_replace($search, '', $string_to_check)
die('Only letters allowed');
maybe it's faster than ereg or preg
didn't know that checking the way i suggested is that slow
why is that?
i think simple regex is a bit faster in that case. i tested it and its like a whole 2/10,000 faster lol.
if (preg_match('/[^\w]/', $string_to_check)) echo 'only letters';