View Full Version : Taking last character from each line on a file.

11-06-2012, 02:04 PM
So I have tried and tried and just can't get it to work, if somebody could help.. would be great... I have a file and it has many lines, I want to take the last character of all these lines and echo them with each one being on a new line.. obviously this doesnt work and im really quite bad at php.. but it was a attempt.

$str = file_get_contents("problems.txt");

$arr = explode("*", $str);

foreach ($arr as $value) {
$last = $value[strlen($value)-1];

echo $last;


11-06-2012, 02:59 PM
How about

$lines = file("problems.txt");

foreach ($lines as $line) {
$last = substr($line,strpos($line,PHP_EOL)-1);
echo $last.'<br>';

11-07-2012, 12:45 AM
Thank you so so much.... =)