citricsquid Posted February 14, 2010 Share Posted February 14, 2010 Let's say I have the following strings: dsfdfsfdd/48977856+FE_F)E_E/dfhjdjhsd/sdffdsfsd/dsfsdfsdfp dsfdfsfdd/123p dsfdfsfdd/41234/1234324/3243223/324234 and I want to match the very final value after /, so It'd match: dsfdfsfdd/48977856+FE_F)E_E/dfhjdjhsd/sdffdsfsd/dsfsdfsdfp dsfdfsfdd/123p dsfdfsfdd/41234/1234324/3243223/324234 I know how to find the first, but not the last! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/192041-match-last-character-in-a-string/ Share on other sites More sharing options...
salathe Posted February 14, 2010 Share Posted February 14, 2010 I'll assume you're somewhat familiar with how preg_match works (since you can find the first already) so will skip a longer explanation. Essentially, you can use a regex which looks for the last set of non-forward-slash characters anchored to the end of the string (using $) like in the example below. $subject = 'dsfdfsfdd/41234/1234324/3243223/324234'; preg_match('#[^/]+$#D', $subject, $match); echo $match[0]; // 324234 Do feel free to ask if anything is unclear (like, "dude, what is that capital 'D' doing?!") or let us know if, god forbid, the pattern above isn't cutting it for you. Quote Link to comment https://forums.phpfreaks.com/topic/192041-match-last-character-in-a-string/#findComment-1012141 Share on other sites More sharing options...
cags Posted February 14, 2010 Share Posted February 14, 2010 It those random strings are actually paths, you could also just use the basename function. Quote Link to comment https://forums.phpfreaks.com/topic/192041-match-last-character-in-a-string/#findComment-1012143 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.