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. Link to comment https://forums.phpfreaks.com/topic/47902-backwards-substr/ 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. Link to comment https://forums.phpfreaks.com/topic/47902-backwards-substr/#findComment-234101 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); Link to comment https://forums.phpfreaks.com/topic/47902-backwards-substr/#findComment-234126 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. Link to comment https://forums.phpfreaks.com/topic/47902-backwards-substr/#findComment-234133 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.