preg_match() gives one match.
preg_match_all() returns all matches.
preg_split() returns all splits.
How can I split only on the first match?
Example:
preg_split("~n~", $string);
This is what I want:
array(
0 => 'first piece',
1 => 'the rest of the string
without any further splits'
)
Advertisement
Answer
Simply set $limit to 2 for 2 parts of the array. Thanks to @BenJames 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.