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 199 views
1 answer 213 views
2 answers 241 views
3 answers 823 views
1 answer 210 views
1 answer 189 views
3 answers 329 views
...