0

I am trying to get the ASCII value of some character that are in extend ASCII character set.

Like:

echo ord('„');

Its output is: 226

But actual ASCII value is : 132.

My question is how to get the actual ASCII value of those character that are greater than 1 byte size?

Rashad
  • 11,057
  • 4
  • 45
  • 73

2 Answers2

0

ord simply takes the first byte of the given string and returns its numerical value in decimal form. If it doesn't give you what you expect, likely your input is not what you expect. If you want the byte value of Extended ASCII, then your input string must be encoded in Extended ASCII. Currently you're likely getting the value of the first byte of E2 80 9E, the UTF-8 encoding for "„", because your input is actually UTF-8 encoded, because your source code file is saved as UTF-8.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • Can you give any practical example. Btw thanks for the info. :) – Rashad Mar 04 '15 at 09:06
  • Practical example of what exactly? Go to your text editor and save your code file encoded as Extended ASCII (or likely Latin-1 or such), then you'll get your expected result. However, I don't know what you *really* want to achieve here, maybe you actually need something else entirely. – deceze Mar 04 '15 at 09:17
0

I found the solution here. Your character is 8222 in utf8 encoding and it is called multibyte character (mb) or html special entity.

function mb_ord($string)
{
    if (extension_loaded('mbstring') === true)
    {
        mb_language('Neutral');
        mb_internal_encoding('UTF-8');
        mb_detect_order(array('UTF-8', 'ISO-8859-15', 'ISO-8859-1', 'ASCII'));

        $result = unpack('N', mb_convert_encoding($string, 'UCS-4BE', 'UTF-8'));

        if (is_array($result) === true)
        {
            return $result[1];
        }
    }

    return ord($string);
}
echo mb_ord('„');
Community
  • 1
  • 1
Szaby
  • 39
  • 3
  • Right... but actually 132 is not that character... @Rashad has found a wrong ascii table probably :)... its 8222 in utf encoding – Szaby Mar 04 '15 at 13:56