确定文件夹深度“../”的代码在 PHP 中?
预先感谢您将收到的帮助!
我曾经使用我刚开始时编写的代码,因为无法找到满足我要求的代码片段。 (也许不知道术语)我确信有一种更简单的方法,这就是我寻求建议的原因。
/编辑/ 将使用此模板的页面临时托管在另一个网站的子目录中,并且使用“/”从页面的根目录开始不是我想要的选项,因为我想轻松地将网站放在不同的托管服务器并向页面添加域名,因此需要更新根目录的路径(例如“/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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
应该工作得很好。
Should work just fine.
要在 PHP 中连接字符串,可以使用
.
运算符。试试这个:
To concatenate strings in PHP, you use the
.
operator.Try this: