preg_replace() relative paths to absolute path. (Masking?)
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开发者_如何学C/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,
AndrewFound 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.
I also can't se much sense in your doing, but maybe the PHP function "realpath" is your friend.
精彩评论