Delete the first and last line of a variable in PHP:
Using the phph interactive shell:
php> $test = "line one\nline two\nline three\nline four"; php> $test = substr($test, (strpos($test, "\n")+1)); php> $test = substr($test, 0, strrpos($test, "\n")); php> print $test; line two line three
Perhaps you meant "Last non-empty line." In this case, do the following:
Note that there are three blank lines after the content. This removes these lines before removing the latter:
php> $test = "line one\nline two\nline three\nline four\n\n\n"; php> $test = substr($test, 0, strrpos(trim($test), "\n")); php> print $test; line one line two line three
Eric Leschinski
source share