This is a discussion on find last part of string within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hi, What would be the best syntax to find the last piece of string indicated by an underscore in a ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hi,
What would be the best syntax to find the last piece of string indicated by an underscore in a string ? The string can have multiple underscores however, only the last one is valid. E.g. $mystring = "abcd_123456_QWER_98765" ; $laststring should result in : "98765" and how do I get the amount of characters from $laststring ? |
|
|||
|
Peter wrote: > Hi, > > What would be the best syntax to find the last piece of string indicated by > an underscore in a string ? > The string can have multiple underscores however, only the last one is > valid. > > E.g. > > $mystring = "abcd_123456_QWER_98765" ; > > $laststring should result in : "98765" > > and how do I get the amount of characters from $laststring ? I'm sure a bunch of people will try to use a regular expression to do this, but you don't really need one: $mystring = "abcd_123456_QWER_98765" ; $laststring = substr($mystring, strrpos($mystring, '_')); $numChars = strlen($laststring); |
|
|||
|
Doing some testing.
A small bug it seems, line 2 should be: $laststring = substr($mystring, strrpos($mystring, '_') + 1); > > $mystring = "abcd_123456_QWER_98765" ; > $laststring = substr($mystring, strrpos($mystring, '_')); > $numChars = strlen($laststring); > > |
|
|||
|
Peter wrote: > Doing some testing. > A small bug it seems, line 2 should be: > > $laststring = substr($mystring, strrpos($mystring, '_') + 1); > > > > > $mystring = "abcd_123456_QWER_98765" ; > > $laststring = substr($mystring, strrpos($mystring, '_')); > > $numChars = strlen($laststring); > > > > If I just gave you the code and it worked perfectly, you'd have no reason to look at it and try to understand how it works :) |
|
|||
|
On 6 Dec 2006 09:51:47 -0800, "ZeldorBlat" <zeldorblat@gmail.com>
wrote: > >Peter wrote: >> Doing some testing. >> A small bug it seems, line 2 should be: >> >> $laststring = substr($mystring, strrpos($mystring, '_') + 1); >> >> > >> > $mystring = "abcd_123456_QWER_98765" ; >> > $laststring = substr($mystring, strrpos($mystring, '_')); >> > $numChars = strlen($laststring); >> > >> > > >If I just gave you the code and it worked perfectly, you'd have no >reason to look at it and try to understand how it works :) That's just plain silly. If he examines it and understands how it works, he may be able to progress. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|