tags:

views:

51

answers:

2

In PHP, how do I distinguish between a number as a string [0-9] versus an operator (+-*/) or letter [A-Za-z]?

I tried this, but intval also converts the type of nonnumbers to ints as well:

is_int(intval($somestr));

Is regex the way to do it?

+3  A: 

Use the ctype functions. E.g.:

$isNumeric = ctype_digit('123123');
Coronatus
better answer here.
Sepehr Lajevardi
Except yours is not. He asked how to detect numbers OR letters OR symbols. The is no `is_letter` or `is_symbol` functions like `ctype` has.
Coronatus
"How do I distinguish between a number as a string [0-9] VERSUS an operator (+-*/) OR letter [A-Za-z]?" from this I get what I answered!
Sepehr Lajevardi
These are both pretty useful, but this time, I'll use is_numeric(). ctype_digit() will come in very handy though.
chimerical
Thanks Coronatus and Sepehr!
chimerical
you're welcome ;)
Sepehr Lajevardi
+6  A: 

try is_numeric()

is_numeric gives true by f. ex. 1e3 or 0xf5 too. So it's not the same as ctype_digit, which just gives true when only values from 0 to 9 are entered. ref

Sepehr Lajevardi
Why leave a cheeky comment below my answer when yours isn't what he asked for?
Coronatus
Sepehr Lajevardi