preg_replace() 相对路径到绝对路径。 (掩蔽?)
我正在尝试找出如何掩盖相对路径上的路径。这是我为 CSS 文件实现的自定义缩小器脚本,需要绝对相对路径。
假设我的 CSS 文件中有以下内容 url('../images/file.jpg')
该文件位于目录 /application/module/assets/css/theme 中.css
,这意味着 CSS 文件中的新路径需要为 /application/module/assets/images/file.jpg
。
现在假设 /application/plugins/pluginName/assets/css/plugin.css
中有一个 CSS 文件,并链接到 ../../../../module/assets /images/image.jpg
,替换的路径需要是 /application/module/assets/images/file.jpg
所以我问,是否有一个不错的 preg_replace 设置我可以用来做到这一点:
str_replace('../', '/path/to/file/', $file);
str_replace('../../', '/path/to/', $file);
str_replace('../../../', '/path/', $file);
希望这是有道理的...
问候,
安德鲁
I am in the process of trying to figure out how to mask a path over relative paths. This is a custom minifier script I'm implementing for CSS files, and need to absolute the relative paths.
So say I have the following in a CSS file url('../images/file.jpg')
This file is sitting in the directory /application/module/assets/css/theme.css
, this would mean the new path in the CSS file would need to be /application/module/assets/images/file.jpg
.
Now say there is a CSS file in /application/plugins/pluginName/assets/css/plugin.css
and links to ../../../../module/assets/images/image.jpg
, the replaced path would need to be /application/module/assets/images/file.jpg
So I'm asking, is there a nice preg_replace setup I can use to do this:
str_replace('../', '/path/to/file/', $file);
str_replace('../../', '/path/to/', $file);
str_replace('../../../', '/path/', $file);
Hopefully this makes sense...
Regards,
Andrew
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从另一个问题中找到了这个(PHP:如何解析相对网址 ): PHP 技巧:如何将相对 URL 转换为绝对 URL
我认为您想要的函数是
url_remove_dot_segments
函数,它采用串联路径(例如$base . '/' . $path
)并返回带有的版本//
、/./
和/../
已删除。Found this from another question (PHP: How to resolve a relative url): PHP tip: How to convert a relative URL to an absolute URL
I think that the function you want is the
url_remove_dot_segments
function which takes a concatenated path (e.g.$base . '/' . $path
) and returns a version with//
,/./
and/../
removed.我也看不出你的做法有多大意义,但也许 PHP 函数“realpath”是你的朋友。
I also can't se much sense in your doing, but maybe the PHP function "realpath" is your friend.