Jump to content

Archived

This topic is now archived and is closed to further replies.

Lienne

Get last two chars of a string

Recommended Posts

Like the topic says, I have a string of variable lengths, and I want to find out what the last two characters are. Thanks in advance!

Share this post


Link to post
Share on other sites
the function you want is substr (for substring).

Here is how you would use it to find the last 2 characters of a string:

$string="abcdef";
$rest = substr($string, -2); // returns "ef"

the "-2" means it will take a substring starting from 2 characters before the end, and since no end parameter is given, it takes the whole rest of the string. For more on this function, check [a href=\"http://ca.php.net/substr\" target=\"_blank\"]http://ca.php.net/substr[/a]

Share this post


Link to post
Share on other sites
ooh, ingenius. so Instead of using a positive number to move to the right, we us a negative to move to the left (which is the end). Great! Thank you very much!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.