Jump to content

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:

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.