我需要将一个字符串拆分为 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/