3v4l.org

run code in 300+ PHP versions simultaneously
<?php $texto = "~0 11111111 ~1 222222222 ~2 3333333333 ~end ~0 aaaaaaaaaaa ~1 bbbbbbbbbb ~2 cccccccccc ~3 ddddddddddd ~end ~0 yyyyyyyyyyy xxxxxxxx ffffffffff ~1 rrrrrrrrrrrr ~end"; $sub_bloques = []; foreach(array_map('trim', explode("\n", $texto)) as $line) { if ($line && substr($line, 0, 1) !== '~') { $sub_bloques[] = $line; } if ($line == '~end') { $hola[] = $sub_bloques; $sub_bloques = []; } } var_dump($hola);

preferences:
168.21 ms | 1680 KiB | 5 Q