0

How can I get the ASCII code of a character in PHP ??

http://www.backbone.se/urlencodingUTF8.htm

When I try:

$h = dechex(ord('ñ'));

echo $h;

I'm getting C3 when I should be getting f1

I want for example:

for 'ñ' -> %f1
for 'º' -> %ba

How can I get that?

Thanks in advance!!

Adrian Garcia
  • 788
  • 10
  • 14
  • 3
    "ord — Return ASCII value of character" - the PHP Documentation. Keyword here being "ASCII". `'ñ'` is not ASCII (_maybe_ UTF-8 ?). – Sergiu Paraschiv Oct 17 '14 at 12:38
  • `ord` returns ASCII value of character. ASCII doesn't contain the `ñ`. – bzeaman Oct 17 '14 at 12:38
  • *It's not ascii* ? So what is this http://www.theasciicode.com.ar/extended-ascii-code/enie-spanish-enye-lowercase-letter-n-tilde-ascii-code-164.html ? It is from *extended* ascii but it is ascii – hlscalon Oct 17 '14 at 12:43
  • 1
    http://stackoverflow.com/questions/10333098/utf-8-safe-equivelant-of-ord-or-charcodeat-in-php – Sergiu Paraschiv Oct 17 '14 at 12:54

1 Answers1

3

As they told you in the comments, ñ is not standard ASCII. It is present on the extended ASCII table, but your source file may be saved in UTF-8 and thus ñ is stored with different bytes. You could try setting your editor to save documents in a different charset.

However, what you're doing is really dangerous. Since as you see encoding can change, it's always a bad idea to write in source files characters that are not part of standard ASCII.

Why do you need to do that operation? Is there another way? Can't you use UTF-8?

ItalyPaleAle
  • 7,185
  • 6
  • 42
  • 69