如何在 PHP 中获取字符串的字节值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/591446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the byte values of a string in PHP?
提问by lynn
Say I have a string in php, that prints out to a text file like this:
假设我在 php 中有一个字符串,它会打印到这样的文本文件中:
n?§9q1Fa£
n?§9q1Fa£
How do I get the byte codes of this to my text file rather than the funky ascii characters?
如何将这个字节码而不是时髦的 ascii 字符输入到我的文本文件中?
回答by Gautam
Use the ord function
使用 ord 函数
eg.
例如。
<?php
$var = "n?§9q1Fa£??ó§?_?—Ló]j";
for($i = 0; $i < strlen($var); $i++)
{
echo ord($var[$i])."<br/>";
}
?>
回答by Roman Hocke
If You wish to get the string as an array of integer codes, there's a nice one-liner:
如果您希望将字符串作为整数代码数组,有一个很好的单行:
unpack('C*', $string)
Beware, the resulting array is indexed from 1, not from 0!
请注意,结果数组的索引是从 1 开始的,而不是从 0 开始!

