Page 1 of 1

get a part of string - improve my solution

Posted: Thu Mar 19, 2009 5:08 am
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?

Re: get a part of string - improve my solution

Posted: Thu Mar 19, 2009 5:13 am
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.

Re: get a part of string - improve my solution

Posted: Thu Mar 19, 2009 5:14 am
by php_east