doufeixuan8882 2018-08-06 02:55
浏览 52
已采纳

在url中获取basename

I have url

http://localhost:8888/cr/organization/test

How i can get organization from URL?

I'm using

basename($_SERVER['REQUEST_URI']); But it returns just test;

Any way for me to get organization?

  • 写回答

1条回答 默认 最新

  • douliao8760 2018-08-06 03:47
    关注

    You can use pathinfo() to obtain an array that breaks down the url into different components. One of the components is the directory name. Use explode() with the / as the delimiter and that will output an array containing all of the folders in the url.

    Like so:

    $path_parts = pathinfo('http://localhost:8888/cr/organization2/test2');
    
    $folders = explode('/', $path_parts['dirname']);
    
    print_r($folders);
    

    Outputs:

    Array
    (
        [0] => http:
        [1] => 
        [2] => localhost:8888
        [3] => cr
        [4] => organization
    )
    

    As I mentioned earlier pathinfo() provides an array with different components in it. Another component is the basename. That will provide you with the "test2" in your example.

    echo $path_parts['basename']; //Outputs "test2"
    

    Hopefully that will get you started to where you can apply some logic to get the desired results.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效