views:

296

answers:

4

Is there a built in function in php to convert special characters to it's ascii code?

+6  A: 

Yes, the ord function

See the ord manual page

EDIT: there's also chr to do the opposite.

nico
+5  A: 

There is the ord function which returns the ASCII value of a character.

There is also its converse chr which takes an ASCII number and returns the character.

If you are trying to convert characters from one character set to another, then you can use the iconv library

Neil Aitken
+3  A: 

The previous responses are correct, as long as you are using plain ASCII (which means only basic English alphabet lower+uppercase, Arabic numbers and basic English punctuation). Once you are using more than that, character encodings come into play.

First of all, you always need to keep in mind what encoding you're using - some characters don't even exist in some encodings (plain ASCII only contains 127 characters), some exist in one encoding but not another, etc. So you need to know what encoding you're using.

Second, some encodings use multi-byte characters (e.g. utf-8) - that is, one character is stored as one or more bytes. Those don't have an ASCII code, either - see e.g. Joel Spolsky's article on Unicode for more details.

Piskvor
Well put, Character encoding issues can really stuff you if you aren't careful.
Neil Aitken
A: 

Try this function:

function ordUTF8($c, $index = 0, &$bytes = null)
{
    $len = strlen($c);
    $bytes = 0;

    if ($index >= $len)
    {
        return false;
    }

    $h = ord($c{$index});

    if ($h <= 0x7F) 
    {
        return $h;
    }
    else if ($h < 0xC2)
    {
        return false;
    }
    else if ($h <= 0xDF && $index < $len - 1) 
    {
        return ($h & 0x1F) <<  6 | (ord($c{$index + 1}) & 0x3F);
    }
    else if ($h <= 0xEF && $index < $len - 2) 
    {
        return ($h & 0x0F) << 12 | (ord($c{$index + 1}) & 0x3F) << 6
                                | (ord($c{$index + 2}) & 0x3F);
    }           
    else if ($h <= 0xF4 && $index < $len - 3) 
    {
        return ($h & 0x0F) << 18 | (ord($c{$index + 1}) & 0x3F) << 12
                                 | (ord($c{$index + 2}) & 0x3F) << 6
                                 | (ord($c{$index + 3}) & 0x3F);
    }
    else
    {
        return false;
    }
}

The first parameter is the string, the second the starting index (if you specify only one special character, then this will be zero).

Zsolti