Liquid Fire Posted April 20, 2007 Share Posted April 20, 2007 I was wondering if there is a function that does the same thing as substr but starts from the end so when i did substr($string, 0, 4) i would get the last four characters of the string. I know i can do the following to get the last four characters: <?php $last_character = (strlen($string) - 1); $first_character = ($last_character - 4); $sub_string = substr($string, $first_character, $last_character); ?> However if there is something that already does this i rather use that, if not i will just build this function into my file that has all my common functions. Quote Link to comment Share on other sites More sharing options...
per1os Posted April 20, 2007 Share Posted April 20, 2007 <?php $string = "Thelast4"; $lastfour = substr($string, -1, 4); ?> www.php.net/substr it can work backwards too. Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 20, 2007 Share Posted April 20, 2007 <?php $string = "Thelast4"; $lastfour = substr($string, -1, 4); ?> www.php.net/substr it can work backwards too. That wont work. This will: $lastfour = substr($string, -4); Quote Link to comment Share on other sites More sharing options...
obsidian Posted April 20, 2007 Share Posted April 20, 2007 $lastfour = substr($string, -4); That's it exactly. Read the examples in the manual, and you'll see that providing a negative number for the starting point will count backwards from the end of the string. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.