protected function adjustCssUrl($cssContent, $inputFilePath, $outputFilePath)
    {
        $inputFilePath = str_replace('\\', '/', $inputFilePath);
        $outputFilePath = str_replace('\\', '/', $outputFilePath);

        $sharedPathParts = [];
        $inputFilePathParts = explode('/', $inputFilePath);
        $inputFilePathPartsCount = count($inputFilePathParts);
        $outputFilePathParts = explode('/', $outputFilePath);
        $outputFilePathPartsCount = count($outputFilePathParts);
        for ($i = 0; $i < $inputFilePathPartsCount && $i < $outputFilePathPartsCount; $i++) {
            if ($inputFilePathParts[$i] == $outputFilePathParts[$i]) {
                $sharedPathParts[] = $inputFilePathParts[$i];
            } else {
                break;
            }
        }
        $sharedPath = implode('/', $sharedPathParts);

        $inputFileRelativePath = trim(str_replace($sharedPath, '', $inputFilePath), '/');
        $outputFileRelativePath = trim(str_replace($sharedPath, '', $outputFilePath), '/');
        if (empty($inputFileRelativePath)) {
            $inputFileRelativePathParts = [];
        } else {
            $inputFileRelativePathParts = explode('/', $inputFileRelativePath);
        }
        if (empty($outputFileRelativePath)) {
            $outputFileRelativePathParts = [];
        } else {
            $outputFileRelativePathParts = explode('/', $outputFileRelativePath);
        }

        $callback = function ($matches) use ($inputFileRelativePathParts, $outputFileRelativePathParts) {
            $fullMatch = $matches[0];
            $inputUrl = $matches[1];

            if (strncmp($inputUrl, '/', 1) === 0 || strncmp($inputUrl, '#', 1) === 0 || preg_match('/^https?:\/\//i', $inputUrl) || preg_match('/^data:/i', $inputUrl)) {
                return $fullMatch;
            }
            if ($inputFileRelativePathParts === $outputFileRelativePathParts) {
                return $fullMatch;
            }

            if (empty($outputFileRelativePathParts)) {
                $outputUrlParts = [];
            } else {
                $outputUrlParts = array_fill(0, count($outputFileRelativePathParts), '..');
            }
            $outputUrlParts = array_merge($outputUrlParts, $inputFileRelativePathParts);

            if (strpos($inputUrl, '/') !== false) {
                $inputUrlParts = explode('/', $inputUrl);
                foreach ($inputUrlParts as $key => $inputUrlPart) {
                    if ($inputUrlPart === '..') {
                        array_pop($outputUrlParts);
                        unset($inputUrlParts[$key]);
                    }
                }
                $outputUrlParts[] = implode('/', $inputUrlParts);
            } else {
                $outputUrlParts[] = $inputUrl;
            }
            $outputUrl = implode('/', $outputUrlParts);

            return str_replace($inputUrl, $outputUrl, $fullMatch);
        };

        $cssContent = preg_replace_callback('/url\(["\']?([^)^"^\']*)["\']?\)/i', $callback, $cssContent);

        return $cssContent;
    }
protected function adjustCssUrl($cssContent, $inputFilePath, $outputFilePath)
    {
        $inputFilePath = str_replace('\\', '/', $inputFilePath);
        $outputFilePath = str_replace('\\', '/', $outputFilePath);

        $sharedPathParts = [];
        $inputFilePathParts = explode('/', $inputFilePath);
        $inputFilePathPartsCount = count($inputFilePathParts);
        $outputFilePathParts = explode('/', $outputFilePath);
        $outputFilePathPartsCount = count($outputFilePathParts);
        for ($i = 0; $i < $inputFilePathPartsCount && $i < $outputFilePathPartsCount; $i++) {
            if ($inputFilePathParts[$i] == $outputFilePathParts[$i]) {
                $sharedPathParts[] = $inputFilePathParts[$i];
            } else {
                break;
            }
        }
        $sharedPath = implode('/', $sharedPathParts);

        $inputFileRelativePath = trim(str_replace($sharedPath, '', $inputFilePath), '/');
        $outputFileRelativePath = trim(str_replace($sharedPath, '', $outputFilePath), '/');
        if (empty($inputFileRelativePath)) {
            $inputFileRelativePathParts = [];
        } else {
            $inputFileRelativePathParts = explode('/', $inputFileRelativePath);
        }
        if (empty($outputFileRelativePath)) {
            $outputFileRelativePathParts = [];
        } else {
            $outputFileRelativePathParts = explode('/', $outputFileRelativePath);
        }

        $callback = function ($matches) use ($inputFileRelativePathParts, $outputFileRelativePathParts) {
            $fullMatch = $matches[0];
            $inputUrl = $matches[1];

            if (strncmp($inputUrl, '/', 1) === 0 || strncmp($inputUrl, '
                return $fullMatch;
            }
            if ($inputFileRelativePathParts === $outputFileRelativePathParts) {
                return $fullMatch;
            }

            if (empty($outputFileRelativePathParts)) {
                $outputUrlParts = [];
            } else {
                $outputUrlParts = array_fill(0, count($outputFileRelativePathParts), '..');
            }
            $outputUrlParts = array_merge($outputUrlParts, $inputFileRelativePathParts);

            if (strpos($inputUrl, '/') !== false) {
                $inputUrlParts = explode('/', $inputUrl);
                foreach ($inputUrlParts as $key => $inputUrlPart) {
                    if ($inputUrlPart === '..') {
                        array_pop($outputUrlParts);
                        unset($inputUrlParts[$key]);
                    }
                }
                $outputUrlParts[] = implode('/', $inputUrlParts);
            } else {
                $outputUrlParts[] = $inputUrl;
            }
            $outputUrl = implode('/', $outputUrlParts);

            return str_replace($inputUrl, $outputUrl, $fullMatch);
        };

        $cssContent = preg_replace_callback('/url\(["\']?([^)^"^\']*)["\']?\)/i', $callback, $cssContent);

        return $cssContent;
    }
protected function adjustCssUrl ( $ cssContent , $ inputFilePath , $ outputFilePath ) { $ inputFilePath = str_replace ( '\\' , '/' , $ inputFilePath ) ; $ outputFilePath = str_replace ( '\\' , '/' , $ outputFilePath ) ; $ sharedPathParts = [ ] ; $ inputFilePathParts = explode ( '/' , $ inputFilePath ) ; $ inputFilePathPartsCount = count ( $ inputFilePathParts ) ; $ outputFilePathParts = explode ( '/' , $ outputFilePath ) ; $ outputFilePathPartsCount = count ( $ outputFilePathParts ) ; for ( $ i = 0 ; $ i < $ inputFilePathPartsCount && $ i < $ outputFilePathPartsCount ; $ i ++ ) { if ( $ inputFilePathParts [ $ i ] == $ outputFilePathParts [ $ i ] ) { $ sharedPathParts [ ] = $ inputFilePathParts [ $ i ] ; } else { break ; } } $ sharedPath = implode ( '/' , $ sharedPathParts ) ; $ inputFileRelativePath = trim ( str_replace ( $ sharedPath , '' , $ inputFilePath ) , '/' ) ; $ outputFileRelativePath = trim ( str_replace ( $ sharedPath , '' , $ outputFilePath ) , '/' ) ; if ( empty ( $ inputFileRelativePath ) ) { $ inputFileRelativePathParts = [ ] ; } else { $ inputFileRelativePathParts = explode ( '/' , $ inputFileRelativePath ) ; } if ( empty ( $ outputFileRelativePath ) ) { $ outputFileRelativePathParts = [ ] ; } else { $ outputFileRelativePathParts = explode ( '/' , $ outputFileRelativePath ) ; } $ callback = function ( $ matches ) use ( $ inputFileRelativePathParts , $ outputFileRelativePathParts ) { $ fullMatch = $ matches [ 0 ] ; $ inputUrl = $ matches [ 1 ] ; if ( strncmp ( $ inputUrl , '/' , 1 ) === 0 || strncmp ( $ inputUrl , '#' , 1 ) === 0 || preg_match ( '/^https?:\/\//i' , $ inputUrl ) || preg_match ( '/^data:/i' , $ inputUrl ) ) { return $ fullMatch ; } if ( $ inputFileRelativePathParts === $ outputFileRelativePathParts ) { return $ fullMatch ; } if ( empty ( $ outputFileRelativePathParts ) ) { $ outputUrlParts = [ ] ; } else { $ outputUrlParts = array_fill ( 0 , count ( $ outputFileRelativePathParts ) , '..' ) ; } $ outputUrlParts = array_merge ( $ outputUrlParts , $ inputFileRelativePathParts ) ; if ( strpos ( $ inputUrl , '/' ) !== false ) { $ inputUrlParts = explode ( '/' , $ inputUrl ) ; foreach ( $ inputUrlParts as $ key => $ inputUrlPart ) { if ( $ inputUrlPart === '..' ) { array_pop ( $ outputUrlParts ) ; unset ( $ inputUrlParts [ $ key ] ) ; } } $ outputUrlParts [ ] = implode ( '/' , $ inputUrlParts ) ; } else { $ outputUrlParts [ ] = $ inputUrl ; } $ outputUrl = implode ( '/' , $ outputUrlParts ) ; return str_replace ( $ inputUrl , $ outputUrl , $ fullMatch ) ; } ; $ cssContent = preg_replace_callback ( '/url\(["\']?([^)^"^\']*)["\']?\)/i' , $ callback , $ cssContent ) ; return $ cssContent ; }
Adjusts CSS content allowing URL references pointing to the original resources.
@param string $cssContent source CSS content.
@param string $inputFilePath input CSS file name.
@param string $outputFilePath output CSS file name.
@return string adjusted CSS content.
Adjusts CSS content allowing URL references pointing to the original resources .