In PHP, I use this pattern to append absolute paths to relative URLs in my CSS file:
$sCssR = preg_replace('#url\((?!\s*[\'"]?(?:https?:)?//)\s*([\'"])?#', "url($1{$sRel}/", $sCss);
The pattern is working well, but data URLs like SVG images are effected too.
Example:
<? ob_start(); ?>
.url { background:url("//www.test.com/img.png"; }
.url { background:url("http://www.test.com/img.png"; }
.url { background:url("../img/test.png"; }
.url { background:url("data:image/svg+xml;utf8,<svg><circle/></svg>"); }
<?
$sCss = ob_get_clean();
$sRel = '//www.test.com/folder';
$sCssR = preg_replace('#url\((?!\s*[\'"]?(?:https?:)?//)\s*([\'"])?#', "url($1{$sRel}/", $sCss);
print $sCssR;
?>
Output will be:
.url { background:url("//www.test.com/img.png"; }
.url { background:url("http://www.test.com/img.png"; }
.url { background:url("//www.test.com/folder/../img/test.png"; }
.url { background:url("//www.test.com/folder/data:image/svg+xml;utf8,<svg><circle/></svg>"); }
Everything is fine, except the last line, which should not be touched and should look like this:
.url { background:url("data:image/svg+xml;utf8,<svg><circle/></svg>"); }
Can anyone help me to adjust the preg_replace pattern so 'data:' URLs won't be touched?