PHP 字符串拆分

标签 php split unpack

我需要将一个字符串拆分为 2、2、3、3 个字符的 block ,并且能够在 Perl 中使用 unpack 做到这一点:

unpack("A2A2A3A3", 'thisisloremipsum');

但是相同的函数在 PHP 中不起作用,它给出了这样的输出:

Array
(
    [A2A3A3] => th
)

如何使用 unpack 来做到这一点?我不想为它编写一个函数,它应该可以通过 unpack 但是如何实现?

提前致谢

最佳答案

引用 unpack 的手册页:

unpack() works slightly different from Perl as the unpacked data is stored in an associative array.
To accomplish this you have to name the different format codes and separate them by a slash /.


这意味着,使用这样的东西:

$a = unpack("A2first/A2second/A3third/A3fourth", 'thisisloremipsum');
var_dump($a);

您将获得以下输出:

array
  'first' => string 'th' (length=2)
  'second' => string 'is' (length=2)
  'third' => string 'isl' (length=3)
  'fourth' => string 'ore' (length=3)

关于PHP 字符串拆分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2599230/

相关文章:

python - 如何将列表拆分为大小相等的 block ?

perl - 为什么这个函数会占用大量内存?

php - 从维基百科模板获取参数

intellij-idea - 如何在 PhpStorm 中调整拆分器的大小

java - splitByWholeSeparator、splitPreserveAllTokens 和 splitByWholeSeparatorPreserveAllTokens 之间的区别

python arp header 用struct模块解包

Perl 密码学 : Encrypting/Decrypting ASCII chracters with pack and unpack functions

javascript - 解析获取查询字符串并使用值来更改复选框状态

php - 将一个结果中的多个数组合并到 PHP 中的单个数组中

php - 错误: Unknown column '' in 'field list' MySQL error