Skip to content

Core\Str::lines

Splits a text into its lines.

Core\Str::lines takes one string and returns an array with one string for each line. The line breaks are not part of the result. A line ends at \n, at \r\n or at a single \r. All three work on every system, so a text written on Windows gives the same lines as one written on Linux.

A line break at the very end of the text does not add an empty line, so "a\nb\n" gives two lines. An empty line in the middle is kept: "a\n\nb" gives three lines, and the second one is "". An empty string gives an empty array.

Core\Str::join with "\n" does the opposite: it puts a list of lines back into one text.

Parameters

  • $sstringThe text to split.

Return value

Returns array<string>. One string for each line, without its line break. A line break at the very end does not add an empty line. An empty line in the middle is kept as "". An empty string gives [].

Examples

Number each line

<?nvs
// Shows how to split a text into lines and print each line with its number.
string $poem = "Roses are red,\nviolets are blue,\n\nsugar is sweet.\n";
// There are four lines. The third line is empty, so it prints as `3: `.
// The line break at the end does not add a fifth line.
array<string> $lines = Core\Str::lines($poem);
uint $number = 1;
foreach ($lines as string $line) {
echo $number, ': ', $line, "\n";
$number = $number + 1;
}
// An empty text has no lines. This prints `0`.
echo Core\Arr::count(Core\Str::lines('')), "\n";
Output
1: Roses are red,
2: violets are blue,
3:
4: sugar is sweet.
0

Every kind of line break

<?nvs
// Shows that `Core\Str::lines` finds the same lines whatever line break a text uses.
// The same two lines, written the Linux way, the Windows way and the old Mac way.
array<string> $texts = ["first\nsecond", "first\r\nsecond", "first\rsecond"];
// Each text gives two lines, and no line keeps a `\r` at its end.
// This prints `first|second` three times.
foreach ($texts as string $text) {
echo Core\Str::join(Core\Str::lines($text), '|'), "\n";
}
// One text can mix all three. This prints `4`.
echo Core\Arr::count(Core\Str::lines("a\nb\r\nc\rd")), "\n";
Output
first|second
first|second
first|second
4

Addresses pasted into a form

<?nvs
// Shows how to read a list of email addresses that a user pasted into a form, one on each line.
// A browser sends the lines of a text field with `\r\n` between them.
string $pasted = "anna@example.com\r\n ben@example.com \r\n\r\ncarla@example.com\r\n";
// `Core\Str::trim` removes the spaces around each address.
// An empty line is skipped, so it does not become an empty address.
array<string> $addresses = [];
foreach (Core\Str::lines($pasted) as string $line) {
string $address = Core\Str::trim($line);
if (!Core\Str::isEmpty($address)) {
$addresses[] = $address;
}
}
// This prints `3 addresses` and then the three addresses, one on each line.
echo Core\Arr::count($addresses), " addresses\n";
echo Core\Str::join($addresses, "\n"), "\n";
Output
3 addresses
anna@example.com
ben@example.com
carla@example.com