How can I get the current web directory from the URL?

后端 未结 5 956
[愿得一人]
[愿得一人] 2021-01-28 03:09

If I have a URL that is http://www.example.com/sites/dir/index.html, I would want to extract the word \"sites\". I know I have to use regular expressions but for some reason my

5条回答
  •  死守一世寂寞
    2021-01-28 03:28

    Use parse_url to get the path from $_SERVER['REQUEST_URI'] and then you could get the path segments with explode:

    $_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
    $segments = explode('/', substr($_SERVER['REQUEST_URI_PATH'], 1));
    
    echo $segments[1];
    

提交回复
热议问题