Jump to content

Match last character in a string.


citricsquid

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/192041-match-last-character-in-a-string/
Share on other sites

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. :shy:

Archived

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

×
×
  • Create New...

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.