PHP: delete the first line of a text and return the rest
explode()
it on the line breaks into an array, shift()
off the first line, and rejoin the rest.
$arr = explode("\n", $t);
array_shift($arr);
echo implode("\n", $arr);
// Prints
// All the rest
// Must remain
If your string is really large, this will use a lot of memory. But if your strings are comparable to your example, it will work fine.
Method 2, using strpos()
echo substr($t, strpos($t, "\n") + 1);
How about preg_replace:
$text = "First line.\nSecond line.\nThird line.";
echo preg_replace('/^.+\n/', '', $text);
This way you don't need to worry about the case where there is no newline in your file.
http://codepad.org/fYZuy4LS
In alternative to the other answers with either explode
& implode
or regular expressions, you can also use strpos()
and substr()
:
function stripFirstLine($text) {
return substr($text, strpos($text, "\n") + 1);
}
echo stripFirstLine("First line.\nSecond line.\nThird line.");
Live example: http://codepad.org/IoonHXE7