get a part of string - improve my solution

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dmn
Forum Newbie
Posts: 1
Joined: Thu Mar 19, 2009 5:02 am

get a part of string - improve my solution

Post by dmn »

hi,
i need to get a last part of url after slash. i wrote this solution but i'm afraid that it's so complicated.

Code: Select all

$string = 'http://domain/folder/required-part';
$value = '/folder/';
$occur =  strpos($string, $value) + strlen($value);
echo substr($string, $occur);
result -> required-part

does anybody know more sophisticated solution?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Re: get a part of string - improve my solution

Post by shiznatix »

Probably the fastest:

Code: Select all

 
$parts = explode('/', 'http://domain/folder/required-part');
echo array_pop($parts);
 
You could also use some form of regex but that would be slower and more complicated.
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: get a part of string - improve my solution

Post by php_east »

Post Reply