Home > Archive > PHP Language > December 2006 > find last part of string
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
find last part of string
|
|
|
| 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 ?
| |
| ZeldorBlat 2006-12-05, 6:57 pm |
|
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);
| |
|
| Hey thanks,
that was fast :)
>
> $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);
>
>
| |
| ZeldorBlat 2006-12-11, 7:01 pm |
|
Peter wrote:[color=darkred]
> Doing some testing.
> A small bug it seems, line 2 should be:
>
> $laststring = substr($mystring, strrpos($mystring, '_') + 1);
>
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 :)
| |
| tehllama@hotmail.co.uk 2006-12-11, 7:01 pm |
| On 6 Dec 2006 09:51:47 -0800, "ZeldorBlat" <zeldorblat@gmail.com>
wrote:
>
>Peter wrote:
>
>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.
|
|
|
|
|