确定文件夹深度“../”的代码在 PHP 中?

发布于 2024-12-17 12:08:51 字数 910 浏览 4 评论 0原文

预先感谢您将收到的帮助!

我曾经使用我刚开始时编写的代码,因为无法找到满足我要求的代码片段。 (也许不知道术语)我确信有一种更简单的方法,这就是我寻求建议的原因。

/编辑/ 将使用此模板的页面临时托管在另一个网站的子目录中,并且使用“/”从页面的根目录开始不是我想要的选项,因为我想轻松地将网站放在不同的托管服务器并向页面添加域名,因此需要更新根目录的路径(例如“/temp-dir/site/images/image.jpg”),而使用以前的目录(例如“/temp-dir/site/images/image.jpg”) “../../../images/image.jpg”)将在现有站点和移动后的新位置工作。 /endEDIT/

我想要实现的是使用以下方法在标头模板中的文件名之前应用适当数量的“../”:

<link href="<?php if(isset($pDepth)){ echo $prev; }?>style.css" />

其中 $prev 将等于先前的目录代码,范围为“../”到“../../../../”取决于每个网页上的变量 $pDepth。例如:

if($pDepth == 3){the output of $prev will equal "../../../"}

我看到可以使用 ++ 更改输出,但是您不能添加字符串,可以吗?我在想:

if($pDepth == 4){

  $i = 0;

  while($i != $pDepth){

    // help needed here
    // add "../" until $prev = "../../../../"

  }
}

我仍在学习编写代码,并且非常感谢任何帮助。再次感谢!

Thanks in advance for the help that will be received!

I used to use a code I made when I first started, due to not being able to find a snippet that does what I want. (Maybe to not knowing the terminology) I'm sure there's a more simple way, which is why I'm asking advice.

/EDIT/
The pages that will use this template are temporarily hosted in a subdirectory of another website, and using "/" to start at the root of the page isn't an option I want to go with, due to wanting to effortlessly drop the website on a different hosting server and add a domain name to the pages so the path from root (ex. "/temp-dir/site/images/image.jpg") will need updated, whereas, using a previous directory (ex. "../../../images/image.jpg") will work from existing site and new location upon move.
/endEDIT/

What I am trying to achieve is applying the proper amount of "../" before the file names in a header template using a method like:

<link href="<?php if(isset($pDepth)){ echo $prev; }?>style.css" />

Where $prev will equal previous directory code ranging from "../" to "../../../../" depending on the variable $pDepth on each web page. For example:

if($pDepth == 3){the output of $prev will equal "../../../"}

I seen where the output can change using ++, but you can't add strings can you? I'm thinking something like:

if($pDepth == 4){

  $i = 0;

  while($i != $pDepth){

    // help needed here
    // add "../" until $prev = "../../../../"

  }
}

I'm still learning to write code, and would appreciate any help. Thanks Again!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

傾旎 2024-12-24 12:08:51
$prev = str_repeat('../', $pDepth); 
$prev = str_repeat('../', $pDepth); 
再可℃爱ぅ一点好了 2024-12-24 12:08:51
$pDepth = $pDepth . "../";

应该工作得很好。

$pDepth = $pDepth . "../";

Should work just fine.

往日情怀 2024-12-24 12:08:51

要在 PHP 中连接字符串,可以使用 . 运算符。
试试这个:

if($pDepth == 4){

  $i = 0;

  while($i != $pDepth){
    $prev .= '../';
    $i++;

  }
}

To concatenate strings in PHP, you use the . operator.
Try this:

if($pDepth == 4){

  $i = 0;

  while($i != $pDepth){
    $prev .= '../';
    $i++;

  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文