[lang-ref] ( split_when_last_element_is_empty ) ( php )
<?php
public function testSplitWhenLastElementIsEmpty(): void
{
$text1 = "a\nb\nc\n";
$text2 = "a\nb\nc\n\n";
// split
$lines1 = explode("\n", $text1);
$lines2 = explode("\n", $text2);
$this->assertCount(4, $lines1); // last empty item is kept
$this->assertCount(5, $lines2);
$this->assertSame('', $lines1[array_key_last($lines1)]);
$this->assertSame('', $lines2[array_key_last($lines2)]);
// trim and split
$lines1 = explode("\n", rtrim($text1, "\n"));
$lines2 = explode("\n", rtrim($text2, "\n"));
$this->assertCount(3, $lines1); // discard trailing newline
$this->assertCount(3, $lines2);
$this->assertNotSame('', $lines1[array_key_last($lines1)]);
$this->assertNotSame('', $lines2[array_key_last($lines2)]);
}