当前位置: 动力学知识库 > 问答 > 编程问答 >

PHP Regex: CSS Relative to Absolute

问题描述:

I have a problem with a difficult regex. I have this expression to detect absolute urls (http and https

/(url {0,}\(( {0,}'| {0,}"|))(?!http|data\:).*?\)/im

What I want to do basically is with preg_replace to prepend the url with a path $path defined in my script. Basically this regex results in two capture groups:

group 1: (url {0,}\(( {0,}'| {0,}"|))(?!http).*?\)

group 2: ( {0,}'| {0,}"|)

How can I match all the way until the uri starts and then prepend it with $path? I can't seem to get the capturing groups right.

网友答案:

You can use something like this:

$re = '/\b url \s*+ \( \s*+ (?| " ([^"]*+) " | \' ([^\']*+) \' | (\S*+) ) \s*+ \) /ix';

$str = preg_replace_callback($re, function ($match) {
    $url = $match[1];
    // do some check on the url
    if(whatever)
        return $match[0]; // return without change

    // do whatever you want with the URL
    // return new url
    return "url(\"$url\")";
}, $str);
分享给朋友:
您可能感兴趣的文章:
随机阅读: