views:

31

answers:

1
+1  Q: 

regexp find match

I'm working on a regexp to find and replace all matches that don't start with url(http

relative paths are going to be converted to full absolut paths ie: url(foo/bar) > url('http://foo/bar')

match:

  • url(foo/bar)
  • url('foo/bar')

don't match:

  • url(http://foo/bar)
  • url('http://foo/bar')

This is what I've come up with so far, but I'm not 100% there

       $fileContents = preg_replace(
            '/url\(("|\')?(?<!(http))(.+?)("|\')?\)/i',
            'url(\'' . $glmBaseUrl . $subDir . '/$3\')',
            $fileContents
        );
+2  A: 

Something like this should suffice:

preg_replace('/url\(\'?(?!http)([^\']+?)\'?\)/',
    "url('$glmBaseUrl$subDir\$1')", $fileContents);

The problem with yours is that you used negative lookbehind instead of negative lookahead. You should also use [^\'] instead of ., otherwise url('http://foo/bar') will match by not matching the optional ', failing the negative lookahead because it's 'http and not http and then matching the single quote with .+?.

Artefacto
veilig