Printing unicode character

1

HTML Entity (decimal)   ₹
HTML Entity (hex)   ₹
How to type in Microsoft Windows    Alt +20B9
UTF-8 (hex) 0xE2 0x82 0xB9 (e282b9)
UTF-8 (binary)  11100010:10000010:10111001
UTF-16 (hex)    0x20B9 (20b9)
UTF-16 (decimal)    8,377
UTF-32 (hex)    0x000020B9 (20b9)
UTF-32 (decimal)    8,377

I need to print this char, I'm doing this:

$str = htmlentities('&#8377');
echo html_entity_decode($str);

but it just outputs &#8377. I'm expecting the char

php
unicode
currency
asked on Stack Overflow Jan 28, 2015 by eozzy

3 Answers

2

A character reference like ₹ is not recognized without the trailing semicolon. Moreover, you can simply use

echo "₹"

or (if you are properly using UTF-8 throughout, which is recommendable) simply

echo "₹";

And quite often you don’t need echo, as you can simply write the reference ₹ or the character in HTML.

Whether browsers render the “₹” character is a different issue; you should normally do some font settings, possibly with a downloadable font, @font-face.

answered on Stack Overflow Jan 28, 2015 by Jukka K. Korpela
1

Figured it:

echo html_entity_decode('₹', ENT_NOQUOTES, 'UTF-8');

answered on Stack Overflow Jan 28, 2015 by eozzy
1
echo "₹";

Works as long as you are outputting to HTML content, where character references apply. No good for other formats (eg injecting into JavaScript).

echo "₹";

Will work fine as long as your source file is saved in UTF-8 encoding.

Otherwise, the UTF-8 byte encoding of character U+20B9 Indian Rupee Sign is 0xE2, 0x82, 0xB9. So:

echo "\xE2\x82\xB9";

Or, using a function to create UTF-8 strings from code point numbers:

function unichr($i) {
    return iconv('UCS-4LE', 'UTF-8', pack('V', $i));
}

echo unichr(0x20B9);
answered on Stack Overflow Jan 28, 2015 by bobince

User contributions licensed under CC BY-SA 3.0