user5780947
user5780947

Reputation:

PHP split up to every nth line into an array

I am attempting to split a log file by up to every nth line instead of every line. Currently I am using preg_split for line breaks which gives me a new array element for each line. I am trying to split by nth line.

$str = file_get_contents('filename');
$arr1 = preg_split("/\r\n|\n|\r/", $str, -1, PREG_SPLIT_NO_EMPTY);    

Upvotes: 1

Views: 1080

Answers (2)

Bizmate
Bizmate

Reputation: 1872

How about

/(.*(\r\n|\n|\r)){5}/g

where you can change the {5} to 2 if you want split by 2 lines instead of 5?

Upvotes: 1

Professor Abronsius
Professor Abronsius

Reputation: 33813

You could alternatively try something like this?

$chunks = array_chunk( file( $filename ), 5 );

Upvotes: 3

Related Questions