drutjkpsr67393592 2013-08-01 11:32
浏览 39
已采纳

如何以二进制安全的方式从多字节PHP字符串变量中获取单个字节?

Let's say (for simplicity's sake) that I have a multibyte, UTF-8 encoded string variable with 3 letters (consisting of 4 bytes):

$original = 'Fön';

Since it's UTF-8, the bytes' hex values are (excluding the BOM):

46 C3 B6 6E

As the $original variable is user-defined, I will need to hande two things:

  1. Get the exact number of bytes (not UTF-8 characters) used in the string, and
  2. A way to access each individual byte (not UTF-8 character).

I would tend to use strlen() to handle "1.", and access the $original variable's bytes with a simple `$original[$byteposition] like this:

<?php
header('Content-Type: text/html; charset=UTF-8');

$original = 'Fön';
$totalbytes = strlen($original);
for($byteposition = 0; $byteposition < $totalbytes; $byteposition++)
{
    $currentbyte = $original[$byteposition];

    /*
        Doesn't work since var_dump shows 3 bytes.
    */
    var_dump($currentbyte);

    /*
        Fails too since "ord" only works on ASCII chars.
        It returns "46 F6 6E"
    */
    printf("%02X", ord($currentbyte));
    echo('<br>');
}

exit();
?>

This proves my initial idea is not working:

  1. var_dump shows 3 bytes
  2. printf fails too since "ord" only works on ASCII chars

How can I get the single bytes from a multibyte PHP string variable in a binary-safe way?

What I am looking for is a binary-safe way to convert UTF-8 string(s) into byte-array(s).

  • 写回答

1条回答 默认 最新

  • dongqiuge5435 2013-08-01 12:12
    关注

    you can get a bytearray by unpacking the utf8_ecoded string $a:

    $a = utf8_encode('Fön');
    $b = unpack('C*', $a); 
    var_dump($b);
    

    used format C* for "unsigned char"

    References

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM