tags:

views:

56

answers:

4

Is there a PHP equivalent of Java's Character.getNumericValue(char c)?

A: 

ord

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
A: 

Is this what you want?

http://us.php.net/ord

Cfreak
I don't think so, as `ord()` is limited to the ASCII character space. That Java method returns a Unicode value.
BoltClock
and getNumericValue doesn't return that value anyways
Erick Robertson
+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
`intval` is indeed interesting. Using base 36 should make it handle letters as well.
casablanca
That's brilliant!
Erick Robertson
A: 

No. There are no baked in equivalents.

zacharydanger