1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to get last character in String?

Discussion in 'PHP' started by kks_krishna, Apr 13, 2008.

Thread Status:
Not open for further replies.
  1. #1
    HI,

    How can i get last character in a String?
     
    kks_krishna, Apr 13, 2008 IP
  2. Xtrm2Matt

    Xtrm2Matt Active Member

    Messages:
    129
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #2
    What I would do is reverse the string and work from that:

    
    <?php
    // String
    $string = "Enter whatever here";
    
    // Reverse the string so you can get the last letter/character easily
    $reverse = strrev( $string );
    
    // Echo or do whatever with it
    echo $reverse{0};
    ?>
    PHP:
    The above would return / echo "e".

    Hope that helps.
     
    Xtrm2Matt, Apr 13, 2008 IP
  3. hogan_h

    hogan_h Peon

    Messages:
    199
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #3
    hogan_h, Apr 13, 2008 IP
  4. Xtrm2Matt

    Xtrm2Matt Active Member

    Messages:
    129
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #4
    The above is no doubt a lot easier than mine :)
     
    Xtrm2Matt, Apr 13, 2008 IP
  5. Vbot

    Vbot Peon

    Messages:
    107
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    OR
    $str = "My string";
    echo substr($str, -1);
    PHP:
     
    Vbot, Apr 13, 2008 IP
  6. kks_krishna

    kks_krishna Active Member

    Messages:
    1,495
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    80
    #6
    Thanks for the replys.
     
    kks_krishna, Apr 14, 2008 IP
  7. binh

    binh Peon

    Messages:
    180
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Another way is this:
    $str = "something";
    echo $str[strlen($str)-1];
    
    Code (markup):
     
    binh, May 9, 2009 IP
  8. zealot777

    zealot777 Peon

    Messages:
    448
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Found this the easiest..Thanks!
     
    zealot777, Nov 6, 2009 IP
Thread Status:
Not open for further replies.