Is there a PHP equivalent of Java's Character.getNumericValue(char c)
?
views:
56answers:
4
A:
Edit: Oops, that's not what getNumericValue
does. I guess the answer is no then. You'll have to make up a table of your own that maps numeric characters to numbers.
If you want a function that works with the most common numeric characters, you could do something like this, but it would fail for special Unicode numerals:
function getNumericValue($ch) {
if (ctype_digit($ch))
return ord($ch) - ord('0');
if (ctype_upper($ch))
return ord($ch) - ord('A') + 10;
if (ctype_lower($ch))
return ord($ch) - ord('a') + 10;
return -1;
}
casablanca
2010-10-14 18:25:11
I don't think so, as `ord()` is limited to the ASCII character space. That Java method returns a Unicode value.
BoltClock
2010-10-14 18:26:57
and getNumericValue doesn't return that value anyways
Erick Robertson
2010-10-14 18:31:17
+2
A:
Use the intval()
function.
This will not handle letters or roman numerals the same way, but you could create your own method to do that for those cases. It will handle standard digits, though.
if (intval("2") === 2) echo("YAY!");
Erick Robertson
2010-10-14 18:34:10
`intval` is indeed interesting. Using base 36 should make it handle letters as well.
casablanca
2010-10-14 18:38:59