tags:

views:

146

answers:

4

I am looking for a function that would be the alphabetic equivalent of is_numeric. It would return true if there are only letters in the string and false otherwise. Does a built in function exist in PHP?

+8  A: 

You want ctype_alpha()

gnud
+1  A: 

If you're strictly looking for the opposite of is_numeric(), wouldn't !is_numeric() do the job for you? Or am I misunderstanding the question?

ehdv
You're misunderstanding the question. `!is_numeric("ab12") === true`. He wants to check if a string contains _only_ letters.
gnud
Ah, I see. Thanks for clearing that up.
ehdv
A: 

I would have used preg_match. But that's because I'd never heard of ctype_alpha().

if(!preg_match("/[^a-zA-Z]/", $teststring)) {
    echo "There are only letters in this string";
} else {
    echo "There are non-letters in this string";
}
Erik
If you have special needs, like only letters + commas and spaces, then a regexp is your best choise :)
gnud
I love/hate it when I'm typing up an answer, and I load in new answers only to see one much better then the one I was writing.
Erik
A: 

!is_numeric((float)$variableToBeChecked); also preg_match('#^[a-z]+$#i',$variableToBeChecked); // for one or more letter character

eGaMaL