Se ho un URL simile a questo:
http: //wwww.example/test1/test2/test3/
Come posso recuperare string test3
sopra?
$str = explode("/","http://wwww.example/test1/test2/test3/"); echo $str[count($str)-2];
DEMO: https://eval.in/83914
Soluzione di espressione regolare.
$url = 'http://wwww.example/test1/test2/test3/'; preg_match('#.*/(.*)/$#', $url, $matches); echo $matches[1];
dimostrazione
Usando il gruppo di cattura, $
:
preg_match('!/([^/]+)/[^/]*$!', 'http://wwww.example/test1/test2/test3/', $matches); echo $matches[1];
DEMO: http://ideone.com/7EVfZa