find last part of string

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


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-05-2006
Peter
 
Posts: n/a
Default 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 ?


Reply With Quote
  #2 (permalink)  
Old 12-05-2006
ZeldorBlat
 
Posts: n/a
Default Re: find last part of string


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);

Reply With Quote
  #3 (permalink)  
Old 12-05-2006
Peter
 
Posts: n/a
Default Re: find last part of string

Hey thanks,
that was fast :)

>
> $mystring = "abcd_123456_QWER_98765" ;
> $laststring = substr($mystring, strrpos($mystring, '_'));
> $numChars = strlen($laststring);
>
>



Reply With Quote
  #4 (permalink)  
Old 12-05-2006
Peter
 
Posts: n/a
Default Re: find last part of string

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);
>
>



Reply With Quote
  #5 (permalink)  
Old 12-06-2006
ZeldorBlat
 
Posts: n/a
Default Re: find last part of string


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 :)

Reply With Quote
  #6 (permalink)  
Old 12-07-2006
tehllama@hotmail.co.uk
 
Posts: n/a
Default Re: find last part of string

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.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 02:43 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0