How to get the last part of a URL after the last slash (/) in PHP

2 Answers

0 votes
$url = "http://www.website.com/abc/xyz";
         
$s = substr($url, strrpos($url, '/') + 1);
 
echo $s;
 
 

  
/*
run: 
 
xyz
 
*/
    

 



answered Feb 21, 2020 by avibootz
0 votes
$url = "http://www.website.com/abc/xyz";
         
preg_match("/[^\/]+$/", $url, $matches);
$s = $matches[0]; 
 
echo $s;
 
 

  
/*
run: 
 
xyz
 
*/
    

 



answered Feb 21, 2020 by avibootz

Related questions

1 answer 212 views
1 answer 230 views
2 answers 266 views
3 answers 891 views
1 answer 224 views
1 answer 206 views
3 answers 357 views
...