Вопрос

I have text with multiple lines. Say, its 100 lines. Every four lines together make a single block of data. So, I want to explode data into array after every 4 lines. is there any thing in PHP to achieve this thing?

Это было полезно?

Решение

Not directly. But you can split first the data and then re-implode() it in chucks of four:

$split = explode("\n", $data);
while (!empty($split)) {
    $array[] = implode("\n", array_splice($split, 0, 4));
}

Другие советы

Here you go:

$tmp = explode("\n", $data);
$fours = array();
for($i = 0; $i < count($tmp); $i++)
    if ($i % 4 == 0)
        $fours[count($fours)] = $tmp[$i];
    else
        $fours[count($fours) - 1] .= "\n" . $tmp[$i];
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top