Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Mar 2011
    Posts
    57
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Taking last character from each line on a file.

    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.


    PHP Code:
    <?php
    $str 
    file_get_contents("problems.txt");

    $arr explode("*"$str);


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

    echo 
    $last;
    }



    ?>

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,849
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    How about
    PHP Code:
    $lines file("problems.txt");

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

    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New Coder
    Join Date
    Mar 2011
    Posts
    57
    Thanks
    5
    Thanked 3 Times in 3 Posts
    Thank you so so much.... =)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •