PHP 字符串分割
我需要将字符串拆分为 2,2,3,3 个字符的块,并且能够通过使用 unpack 在 Perl 中执行此操作:
unpack("A2A2A3A3", 'thisisloremipsum');
但是相同的函数在 PHP 中不起作用,它给出以下输出:
Array
(
[A2A3A3] => th
)
我怎样才能做到这一点使用解包?我不想为它编写一个函数,它应该可以通过 unpack 但如何实现呢?
提前致谢,
I need to split a string into chunks of 2,2,3,3 characters and was able to do so in Perl by using unpack:
unpack("A2A2A3A3", 'thisisloremipsum');
However the same function does not work in PHP, it gives this output:
Array
(
[A2A3A3] => th
)
How can I do this by using unpack? I don't want to write a function for it, it should be possible with unpack but how?
Thanks in advance,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
引用
unpack
的手册页:Which means that, using something like this :
您将得到以下输出:
Quoting the manual page of
unpack
:Which means that, using something like this :
You'll get the following output :
我从未使用过此函数,但根据文档,
A
字符表示“空格填充的字符串”。所以我大胆猜测它只采用第一个单词的前两个字符。您是否尝试过
unpack("A2A2A3A3", 'this is lorem ipsum');
?I've never used this function, but according to the documentation, the
A
character means "SPACE-padded string". So I'd hazard a guess that it's only taking the first two characters of the first word.Have you tried
unpack("A2A2A3A3", 'this is lorem ipsum');
?