PHP line splitting

I need to break a string into pieces of 2,2,3,3 characters and was able to do this in Perl using decompression:

unpack("A2A2A3A3", 'thisisloremipsum');

However, the same function does not work in PHP, it gives this result:

Array
(
    [A2A3A3] => th
)

How to do this using unpacking? I do not want to write a function for it, it should be possible with unpacking, but how?

Thanks in advance,

+3
source share
2 answers

Indication of the manual page : unpack

unpack()It works somewhat differently from Perl, because the decompressed data is stored in an associative array.
To do this, you must name the different format codes and divide 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)
+6

, A "SPACE-padded string". , .

unpack("A2A2A3A3", 'this is lorem ipsum');?

0

Source: https://habr.com/ru/post/1740303/


All Articles