0

我拼命想弄清楚如何从特定字符串中获取所有路径和文件名。

例如

src: url('../fonts/fontawesome-webfont.eot?#iefix&v=4.0.1') format('embedded-opentype'), url('../fonts/fontawesome-webfont.woff?v=4.0.1') format('woff'), url('../fonts/fontawesome-webfont.ttf?v=4.0.1') format('truetype'), url('../fonts/fontawesome-webfont.svg?v=4.0.1#fontawesomeregular') format('svg');

应该产生一个包含以下内容的数组:

../fonts/, fontawesome-webfont.eot?#iefix&v=4.0.1
../fonts/, fontawesome-webfont.woff?v=4.0.1
../fonts/, fontawesome-webfont.ttf?v=4.0.1
../fonts/, fontawesome-webfont.svg?v=4.0.1#fontawesomeregular

到目前为止,我得到的是遵循正则表达式:

url\(\'(.*?)\/(.*?)\'

问题是我得到以下信息:

.., /fonts/fontawesome-webfont.eot?v=4.0.1
.., /fonts/fontawesome-webfont.woff?v=4.0.1
.., /fonts/fontawesome-webfont.ttf?v=4.0.1
.., /fonts/fontawesome-webfont.svg?v=4.0.1#fontawesomeregular

那么,为了在路径中最后一次出现 / ,我必须考虑什么?

谢谢你的帮助,p

4

3 回答 3

0

这应该有效:

$s = "src: url('../fonts/fontawesome-webfont.eot?#iefix&v=4.0.1') format('embedded-opentype'), url('../fonts/fontawesome-webfont.woff?v=4.0.1') format('woff'), url('../fonts/fontawesome-webfont.ttf?v=4.0.1') format('truetype'), url('../fonts/fontawesome-webfont.svg?v=4.0.1#fontawesomeregular') format('svg');";

if (preg_match_all("~url *\('(\.\./fonts/)(.*?)'\)~i", $s, $arr)) {
   print_r($arr[1]);
   print_r($arr[2]);
}

输出:

Array
(
    [0] => ../fonts/
    [1] => ../fonts/
    [2] => ../fonts/
    [3] => ../fonts/
)
Array
(
    [0] => fontawesome-webfont.eot?#iefix&v=4.0.1
    [1] => fontawesome-webfont.woff?v=4.0.1
    [2] => fontawesome-webfont.ttf?v=4.0.1
    [3] => fontawesome-webfont.svg?v=4.0.1#fontawesomeregular
)
于 2013-10-28T11:15:30.960 回答
0

我在这里测试过https://www.debuggex.com/并且有效:

([.][.].?)\/(.*?)\'
于 2013-10-28T11:23:16.260 回答
0

对于这种特殊情况,这个正则表达式应该可以完成工作。

((?<=url\(\')(?:[^'\/]+\/)+)([^\/']+(?=\'))

在此处查看演示

于 2013-10-28T11:33:51.277 回答