Detecting whether number string or operator/letter string in PHP?
In PHP, how do I disting开发者_StackOverflowuish 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?
Use the ctype functions. E.g.:
$isNumeric = ctype_digit('123123');
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.
精彩评论