如何从css文件中的图像、字体等获取url路径


How to get url path from images,font,etc in css files?

我试着从css文件中获取这个:

url(/myServer/img/mosaico.png)

我试着用这个:

preg_match_all('/url'((["'']?)((?!["'']?[data:]?).*?'.(gif|png|jpg|jpeg|svg|woff|eot|ttf))''1')?/i', $content, $matches, PREG_SET_ORDER))

但它不起作用,表达式只捕获一些事件。问题出在哪里?

我还需要得到括号中的零件的正则表达式,比如:

array(
    (int) 0 => 'url(/img/arrow-top-white.png)',
    (int) 1 => '',
    (int) 2 => '/img/arrow-top-white.png',
    (int) 3 => 'png'
)

您可以使用下面的正则表达式来匹配上面提到的所有字符串。

url'(((?:'/[^'/()]+?)+?'.(gif|png|jpg|jpeg|svg|woff|eot|ttf))[^)]*')

演示

<?php
$content = "url(/myServer/img/mosaico.png)";
preg_match_all('~url'(((?:'/[^'/]+)+'.(gif|png|jpg|jpeg|svg|woff|eot|ttf))')~', $content, $matches, PREG_SET_ORDER);
print_r($matches);
?>

输出:

Array
(
    [0] => Array
        (
            [0] => url(/myServer/img/mosaico.png)
            [1] => /myServer/img/mosaico.png
            [2] => png
        )
)