If I have a URL look like this:
http://wwww.example/test1/test2/test3/
How can I retrieve string test3
from the url above?
If I have a URL look like this:
http://wwww.example/test1/test2/test3/
How can I retrieve string test3
from the url above?
$str = explode("/","http://wwww.example/test1/test2/test3/");
echo $str[count($str)-2];
DEMO: https://eval.in/83914
Regular expression solution.
$url = 'http://wwww.example/test1/test2/test3/';
preg_match('#.*/(.*)/$#', $url, $matches);
echo $matches[1];
Using capturing group, $
:
preg_match('!/([^/]+)/[^/]*$!', 'http://wwww.example/test1/test2/test3/', $matches);
echo $matches[1];
DEMO: http://ideone.com/7EVfZa
So basename()
does exactly that.
$url = 'http://www.example/test1/test2/test3/';
echo basename($url); // returns : test3;