Php 从CSS文件中提取@font-face的字体系列和字体权重

Php 从CSS文件中提取@font-face的字体系列和字体权重,php,regex,Php,Regex,我有一个函数,可以循环遍历目录中的所有CSS文件,并查找@font-face 如果找到任何@font-face块,我想从中提取font-family和font-weight。 下面的代码是我到目前为止所做的: function get_local_fonts() { $output = array(); $path_to_search = trailingslashit(get_template_directory()); foreach(glob_recursive($

我有一个函数,可以循环遍历目录中的所有CSS文件,并查找
@font-face

如果找到任何
@font-face
块,我想从中提取
font-family
font-weight

下面的代码是我到目前为止所做的:

function get_local_fonts() {
    $output = array();
    $path_to_search = trailingslashit(get_template_directory());
    foreach(glob_recursive($path_to_search . '*.css') as $file) {
        $content = file_get_contents($file);
        preg_match('/(\@font-face)([^}]+)(\})/', $content, $matches, PREG_OFFSET_CAPTURE);

        if (!empty($matches)) {
            preg_match('/(font-family:)([^;]*)/', $matches[2][0], $font_family, PREG_OFFSET_CAPTURE);
            preg_match('/(font-weight:)([^;]*)/', $matches[2][0], $font_weight, PREG_OFFSET_CAPTURE);

            $output[] = array(
                'file_path' => $file,
                'font-family' => trim($font_family[2][0]),
                'font-weight' => array(trim($font_weight[2][0])),
            );
        }
    }

    return $output;
}

这个
preg\u匹配('/(\@font-face)([^}]+)(\})/',$content,$matches,preg\u OFFSET\u CAPTURE)
返回包含
@font-face{…}


我的函数可以很好地处理那些只与
@font-face
有一个匹配项的CSS文件,但是如果一个CSS文件中有4个与@font-face匹配项,我的函数只会添加其中一个。
例如(css文件内容):

@font-face
{
    font-family: "din";
    src: url("din-webfont.eot");
    src: url("din-webfont.eot?#iefix") format("embedded-opentype"),  url("din-webfont.woff") format("woff"), url("din-webfont.ttf") format("truetype");
    font-weight: normal;
    font-style: normal;
}
@font-face
{
    font-family: "din";
    src: url("din_bold-webfont.eot");
    src: url("din_bold-webfont.eot?#iefix") format("embedded-opentype"), url("din_bold-webfont.woff") format("woff"), url("din_bold-webfont.ttf") format("truetype");
    font-weight: bold;
    font-style: normal;
}
我希望得到如下输出:

Array
(
    [0] => Array
        (
            [font-family] => 'din'
            [font-weight] => Array
                (
                    [0] => 'normal',
                    [1] => 'bold'
                )

        )
)
请注意,可能有
@font-face
具有相同的
字体系列
,但不同的
字体重量


结论:
如何循环浏览所有包含

@font-face { 
    font-family: something;
    font-weight: something;
}

并从中提取
font-family
font-weight

使用如下方法:

$fontregex = '~@font-face[^}]*?font-family:\s*"([^"]+)"[^}]*?font-weight:\s*([^;]+);[^}]*?font-style:\s*([^;]+);~';
preg_match_all($fontregex, $mycss, $matches,PREG_SET_ORDER);
print_r($matches);
请参阅底部的输出。 您必须以适合您的方式将
$matches
的结果扇形到最终数组中

目前,结构如下:

Array
(
    [0] => Array
        (
            [0] => @font-face
{
    font-family: "din";
    src: url("din-webfont.eot");
    src: url("din-webfont.eot?#iefix") format("embedded-opentype"),  url("din-webfont.woff") format("woff"), url("din-webfont.ttf") format("truetype");
    font-weight: normal;
    font-style: normal;
            [1] => din
            [2] => normal
            [3] => normal
        )

    [1] => Array
        (
            [0] => @font-face
{
    font-family: "din";
    src: url("din_bold-webfont.eot");
    src: url("din_bold-webfont.eot?#iefix") format("embedded-opentype"), url("din_bold-webfont.woff") format("woff"), url("din_bold-webfont.ttf") format("truetype");
    font-weight: bold;
    font-style: normal;
            [1] => din
            [2] => bold
            [3] => normal
        )

)

好的,我想我必须使用
preg\u match\u all
而不是
preg\u match
。。。不过,如果你有任何改进我的正则表达式模式的建议,那就太好了