printf() Extended Unicode Characters?

孤街醉人 提交于 2019-12-25 07:00:22

问题


$formatthis = 219;
$printthis = 98;
// %c - the argument is treated as an integer, and presented as the character with 
        that ASCII value.

$string = 'There are %c treated as integer %c';
echo printf($string, $formatthis, $printthis);

I'm attempting to understand printf(). I don't quite understand the parameters.

I can see that the first parameter seems to be the string that the formatting will be applied to.

The second is the first variable to format, and the third seems to be the second variable to format.

What I don't understand is how to get it to print unicode characters that are special. E.G. Beyond a-z, A-Z, !@#$%^&*(){}" ETC.

Also, why does it out put with the location of the last quote in the string?

OUTPUT: There are � treated as integer �32

How could I encode this in to UTF-16 (Dec) // Snowman = 9,731 DEC UTF 16? 

UTF-8 'LATIN CAPITAL LETTER A' (U+0041) = 41, but if I write in PHP 41 I will get ')' I googled     an ASCII table and it's showing that the number for A is 065...

ASCII is a subset of UTF-8, so if a document is ASCII then it is already UTF-8

If it's already in UTF-8, why are those two numbers different? Also the outputs different..

EDIT, Okay so the chart I'm looking at is obviously displaying the digits in HEX value which I didn't immediately notice, 41 in HEX is ASCII 065


回答1:


%c is basically an int2bin function, meaning it formats a number into its binary representation. This goes up to the decimal number 255, which will be output as the byte 0xFF.

To output, say, the snowman character ☃, you'd need to output the exact bytes necessary to represent it in your encoding of choice. If you chose UTF-8 to encode it, the necessary bytes are E2 98 83:

printf('%c%c%c', 226, 152, 131); // ☃
// or
printf('%c%c%c', 0xE2, 0x98, 0x83); // ☃

The problem in your case is 1) that the bytes you're outputting don't mean anything in the encoding you're interpreting the result as (meaning the byte for 98 doesn't mean anything in UTF-8 at this point, which is why you're seeing a "�") and 2) that you're echoing the result of printf, which outputs 32 (printf returns the number of bytes it output).



来源:https://stackoverflow.com/questions/27476803/printf-extended-unicode-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!