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?
3
-
1Lines in a text file or a string?AbraCadaver– AbraCadaver2014-05-15 14:38:40 +00:00Commented May 15, 2014 at 14:38
-
1Yes, loops and conditions.lejlot– lejlot2014-05-15 14:38:59 +00:00Commented May 15, 2014 at 14:38
-
Also an example of the text.AbraCadaver– AbraCadaver2014-05-15 14:40:11 +00:00Commented May 15, 2014 at 14:40
Add a comment
|
2 Answers
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));
}
1 Comment
bwoebi
@nl-x yes, you're right, explode is guaranteed to return an array with a least one element if valid input is passed.