3

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

Andrew Ellis
  • 1,175
  • 1
  • 12
  • 29
  • Two thoughts: The "minified" version is not really any shorter, and this is more of a compiler than a minifier, since it changes what the CSS actually does. This just seems more trouble than it's worth to me. (What *is* it worth?) – deceze Jan 20 '11 at 23:47

2 Answers2

1

I also can't se much sense in your doing, but maybe the PHP function "realpath" is your friend.

Marko
  • 514
  • 1
  • 4
  • 16
  • Also, if you need the directory in which the file is : `dirname(realpath('./filename.php'))` – Niloct Feb 13 '15 at 17:42
1

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.

Community
  • 1
  • 1
Thai
  • 10,746
  • 2
  • 45
  • 57