tags:

views:

54

answers:

3

How would I remove a line from the end of a string with PHP? Thanks.

+4  A: 

You want to remove whitespace? Try trim(). It's close cousins ltrim and rtrim may be closer to what you want.

Kalium
Thanks I think rtrim did it.
usertest
A: 

If you are looking to remove the last line is a string containing new line character (\n), then you'd do something like this ...

$someString = "This is a test\nAnd Another Test\nAnd another test.";

echo "SomeString BEFORE=".$someString."\n";

// find the position of the last occurrence of a \n
$firstN = strlen($someString) - strpos(strrev($someString), "\n");

// get rid of the last line
$someString = substr($someString, 0, $firstN);

echo "SomeString AFTER=".$someString;
Don Dickinson
A: 

if you're trying to remove a line from an end of a file, you could try using PHP's file() function to read the file (which places it into an array) and then pop the last element. This is assuming that php is recognizing the line endings in your file.