使用preg_match提取css url

时间:2014-05-16 14:26:53

标签: php regex

我使用此函数从css文本获取url列表。它成功但如果css文本包含base64图像网址,它会破坏所有。它应该传递base64图像网址。我该如何解决?

function extractCssUrls( $text )
    {
        $urls=array();
        $matches=array();
        //$url_pattern     = '(([^\\\\\'", \(\)]*(\\\\.)?)+)';
        $url_pattern     = '(([^\\\\\'"\(\)]*(\\\\.)?)+)'; 
        $urlfunc_pattern = 'url\(\s*[\'"]?' . $url_pattern . '[\'"]?\s*\)';
        $pattern         = '/(' .
            '(@import\s*[\'"]' . $url_pattern     . '[\'"])' .
            '|(@import\s*'      . $urlfunc_pattern . ')'      .
            '|('                . $urlfunc_pattern . ')'      .  ')/iu';
        if ( !preg_match_all( $pattern, $text, $matches ) )
            return $urls;

        // @import '...'
        // @import "..."
        foreach ( $matches[3] as $match )
            if ( !empty($match) )
                $urls['import'][] =
                    preg_replace( '/\\\\(.)/u', '\\1', $match );

        // @import url(...)
        // @import url('...')
        // @import url("...")
        foreach ( $matches[7] as $match )
            if ( !empty($match) )
                $urls['import'][] =
                    preg_replace( '/\\\\(.)/u', '\\1', $match );

        // url(...)
        // url('...')
        // url("...")
        foreach ( $matches[11] as $match )
            if ( !empty($match) )
                $urls['property'][] =
                    preg_replace( '/\\\\(.)/u', '\\1', $match );

        $urls=(isset($urls['property']))?$urls['property']:array();

        $urls=array_unique($urls);
        $urls=array_values($urls);

        return $urls;
    }

我发现了这个错误。我从变量,中删除了$url_pattern个字符。  $url_pattern='(([^\\\\\'"\(\)]*(\\\\.)?)+)';

0 个答案:

没有答案