¿Cómo puedo reemplazar la URL de css?

I'm trying to combine multiple css files into a single file (from many different folder in my host, and including external css files). I use the following short code:

Declare array:


Get content and replace url:

foreach ($styles as $style) {
echo preg_replace('/url\(\s*[\'"]?\/?(.+?)[\'"]?\s*\)/i', 'url('.$style['path'].'$1)', file_get_contents($style['path'].$style['name']));

After combined into one css file, i have some css background image url as follows:

url(ttp://mydomain.com/css/../image/background.png) //Internal path - Case 1A
url(http://mydomain.com/js/camera/../../image/background.png) //Internal path - Case 1B
url(http://external.com/assets/css/../../image/background.png) //External path - Case 2

Actual, the internal path (Case 1A, 1B) can display the background image (despite the lack of professionalism), but in the external path (Case 2) cannot display the background image, my question is: How can i replace wrong path with correct path (REPAIR BASE ON CURRENT RESULTS) as:

url(http://mydomain.com/image/background.png) //Correct internal path 
url(http://external.com/image/background.png) //Correct external path 

(I understand the problem is if find a keyword containing '../' will remove keyword and remove 1 string before the keyword contain '*/', , but I can't figure out how to do this). Thanks in advance.

(I have tried find out and tried searching for 1 week before ask new question).

preguntado el 02 de diciembre de 13 a las 19:12

Thanks for all, i have finnd way to fix it. $output = preg_replace('/\w+\/\.\.\//', '', $output); -

1 Respuestas

additionally replace /up-level-directory/../ con /
que sea '/([^\/]+)/../' con / a nivel mundial

$correct_url = preg_replace('|/\w+/\.\./|', '/', $url);

Respondido el 03 de diciembre de 13 a las 18:12

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.