Jump to content


Photo

Get last two chars of a string


  • Please log in to reply
2 replies to this topic

#1 Lienne

Lienne
  • Members
  • PipPip
  • Member
  • 11 posts
  • LocationCalifornia

Posted 04 April 2006 - 05:07 PM

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!
> SELECT * FROM users WHERE clue > 0
0 rows returned

#2 djnrempel

djnrempel
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 04 April 2006 - 05:18 PM

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]

#3 Lienne

Lienne
  • Members
  • PipPip
  • Member
  • 11 posts
  • LocationCalifornia

Posted 04 April 2006 - 05:33 PM

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!
> SELECT * FROM users WHERE clue > 0
0 rows returned




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users