Skip to content
Advertisement

PHP: delete the first line of a text and return the rest

What’s the best way to remove the very first line of a text string and then echo the rest in PHP?

For example.

This is the text string:

$t=<<<EOF
First line to be removed
All the rest
Must remain
EOF;

This is the final output:

All the rest
Must remain

If I was working with a file in Bash I could do easily the next:

sed -i~ 1d target-file

Or:

tail -n +2 source-file > target-file

Any ideas?

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement