PHP正则表达式:CSS相对于绝对

时间:2012-06-14 19:07:34

标签: php css regex relative absolute

我有一个困难的正则表达式的问题。我有这个表达式来检测绝对网址(http和https

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

我想要做的基本上是使用preg_replace来为我的脚本中定义的路径$path添加url。基本上这个正则表达式导致两个捕获组:

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

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

如何在uri开始之前一直匹配,然后用$path作为前缀?我似乎无法使捕获组正确。

1 个答案:

答案 0 :(得分:2)

您可以使用以下内容:

$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);