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

后端 未结 5 952
[愿得一人]
[愿得一人] 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:21

    Just wanted to recommend additionally to check for a prefixed "/" or "\" and to use DIRECTORY_SEPARATOR :

    $testPath = dirname(__FILE__);
    $_testPath = (substr($testPath,0,1)==DIRECTORY_SEPARATOR) ? substr($testPath,1):$testPath;
    $firstDirectory = reset( explode(DIRECTORY_SEPARATOR, dirname($_testPath)) );
    echo $firstDirectory;
    

提交回复
热议问题