PHP utf 编码问题
如何在 PHP 中对 UTF-16BE 格式的字符串进行编码?对于“演示消息!!!”编码字符串应为“00440065006D006F0020004D00650073007300610067006”。另外,我需要将阿拉伯字符编码为这种格式。
How can I encode strings on UTF-16BE format in PHP? For "Demo Message!!!" the encoded string should be '00440065006D006F0020004D00650073007300610067006'. Also, I need to encode Arabic characters to this format.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,这绝对不是UTF-8,它只是一个字符集(即在内存中存储字符串/显示字符串的一种方式)。
这里的内容看起来像是用于构建每个字符的字节的转储。
如果是这样,您可以通过这种方式获取这些字节:
并且您将获得以下输出:
But, once again, this is not UTF-8 : in UTF-8, like you can see in the example I've give, `D` is stored on only one byte : `0x44`
在您发布的内容中,它是使用两个字节存储的:
0x00 0x44
。也许您正在使用某种 UTF-16 ?
经过更多测试和 @aSeptik 的评论后进行编辑:这确实是 UTF-16。
要获得您所获得的转储类型,您必须确保您的字符串以 UTF-16 编码,这可以通过这种方式完成,例如使用
mb_convert_encoding
函数:然后,它是只需迭代构成此字符串的字节并转储它们的值,就像我之前所做的那样:
您将得到以下输出:
哪种看起来像您发布的内容:-)
(您只需删除对
printf
调用中的空格——我将其放在那里以获得更易于阅读的输出=)First of all, this is absolutly not UTF-8, which is just a charset (i.e. a way to store strings in memory / display them).
WHat you have here looks like a dump of the bytes that are used to build each characters.
If so, you could get those bytes this way :
And you'd get the following output :
But, once again, this is not UTF-8 : in UTF-8, like you can see in the example I've give, `D` is stored on only one byte : `0x44`
In what you posted, it's stored using two Bytes :
0x00 0x44
.Maybe you're using some kind of UTF-16 ?
EDIT after a bit more testing and @aSeptik's comment : this is indeed UTF-16.
To get the kind of dump you're getting, you'll have to make sure your string is encoded in UTF-16, which could be done this way, using, for example, the
mb_convert_encoding
function :Then, it's just a matter of iterating over the bytes that make this string, and dumping their values, like I did before :
And you'll get the following output :
Which kind of looks like what youy posted :-)
(you just have to remove the space in the call to
printf
-- I let it there to get an easier to read output=)例如,通过使用 mbstring 扩展及其 mb_convert_encoding() 函数。
或者
使用 iconv()
E.g. by using the mbstring extension and its mb_convert_encoding() function.
prints
Or by using iconv()