phpsplitnewline

Split multiline text (only once) on the Nth occurrence of a newline


I have 20 lines of string seperated with "\n".

I have 2 fields to put this string into:

How do I run a php code to separate the string into 2 fields?

The 1st-10th line will be stored in $field1.

The 11th-? line will carry over to $field2.


Solution

  • $array = explode("\n", $text);
    $field1 = implode("\n", array_slice($array, 0, 10));
    $field2 = implode("\n", array_slice($array, 10));