我一直在尝试使用各种字符串操作函数来获取URL的第一个子目录,并且遇到了很多麻烦。我想知道是否有人知道一种简单的方法来实现这一目标?
我感谢任何建议,在此先感谢!
http://www.domain.com/pages/images/apple.png //output: pages
www.domain.com/pages/b/c/images/car.png // output: pages
domain.com/one/apple.png // output: one
您可以使用php函数parse_url() ;
$url = 'domain.com/one/apple.png';
$path = parse_url($url, PHP_URL_PATH);
$firstSubDir = explode('/', $path)[1]; // [0] is the domain [1] is the first subdirectory, etc.
echo $firstSubDir; //one
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句