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
    Regular Coder
    Join Date
    Sep 2011
    Posts
    428
    Thanks
    18
    Thanked 26 Times in 26 Posts

    get the last character of a string?

    I'm wondering how you can grab the last character of a string.
    For example:
    PHP Code:
    $string "The quick brown fox jumps over the lazy dog";

    echo 
    $string{0};
    //Echoes the letter T

    echo $string{5};
    //Echoes the letter u 
    My question is, how can you get the LAST character of the string without knowing its length? I tried using -1 but ad no luck (unless it was in another part that it came out wrong). If their isn't another way I could always use strlength and get it but I'm just wondering if theirs an actual way for this

  • #2
    Regular Coder
    Join Date
    Jul 2010
    Location
    Oregon City
    Posts
    280
    Thanks
    5
    Thanked 50 Times in 49 Posts
    PHP Code:
     echo substr($string, -11); 
    or

    PHP Code:
    echo $string[strlen($string)-1]; 
    Last edited by Adee; 11-22-2011 at 02:57 AM.

  • #3
    Regular Coder
    Join Date
    Sep 2011
    Posts
    103
    Thanks
    0
    Thanked 14 Times in 14 Posts
    Check this manual

    http://php.net/manual/en/function.substr.php

    This will help you in future with functions


  •  

    Posting Permissions

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