php - 使用 php 获取 URL 目录的最佳方法是什么?

标签 php

我正在尝试使用 php 在导航中设置事件类。现在我需要按目录而不是完整的 URL 来设置它,因为每个目录都有一个主登录页面,目录中的其他页面有子导航。我打算使用以下内容,但这会返回完整的 url。

function curPageURL() {
$pageURL = 'http';
    if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}

return $pageURL;}

 function activeLink($pageID) {
if ( $pageID == curPageURL() ) {
    echo 'class="active"';
} }

然后我在这样的导航项中调用 activeLink():

<li class="projectchild padding1"><a href="http://www.strangeleaves.com/nexus/index.php" <?php activelink('http://www.strangeleaves.com/nexus/index.php'); ?> >nexus</a></li>

非常感谢您提出有关如何修改它以返回目录的建议。

最佳答案

如果您想从URL 中提取目录名,您可以使用parse_urldirname作为:

$url = 'http://www.strangeleaves.com/nexus/index.php';
$arr = parse_url($url);
$path = dirname($arr['path']); // $path is now /nexus

关于php - 使用 php 获取 URL 目录的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3789143/

相关文章:

php - 如何将数据添加到数据库? PHP MySQL

php - Magento 无法使用正确的用户名和密码登录管理员

php - 配置 nginx 服务器以处理来自多个域的请求

php - 返回 MySQL "group by"限制组

php - 获取所有类别的帖子

php - 如何检查一个字符串是否已经存在于mysql中(所有词序选项)?

php - Composer 忽略安装程序路径配置

php - 使用 jquery fullcalender 通过数据库值将值附加到粗体

php - php mysql 查询中的当前行 ID 或行号

php - 参数绑定(bind)必须更安全(PDO)