Simply set $limit to 2
for 2 parts of the array. Thanks to Ben James for mentioning:
preg_split("~\n~",$string, 2);
I tested and it works fine.
The limit argument:
If specified, then only substrings up to limit are returned with the rest of the string being placed in the last substring. A limit of -1, 0 or null means "no limit" and, as is standard across PHP, you can use null to skip to the flags parameter.
Update:
If it is only new line you want to split:
$fp = strpos($string,"\n");
$arr = new array(substr($string,0,$fp), substr($string,$fp));
Or you can if you insist on preg_split();
:
$fp = strpos($string,"\n");
$arr = preg_split("~\n~",$string,$fp);
There's no preg_split_once()
or any equivalent.