从CSS文件中提取@font-face的font-family和font-weight

[英]Extract font-family and font-weight of @font-face from a CSS file


I have a function that loops through all CSS files in directory and looks for @font-face.
if any @font-face block found, I want to extract font-family and font-weight from it.
the code below is what I've done so far:

我有一个函数循环遍历目录中的所有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;
}


this preg_match('/(\@font-face)([^}]+)(\})/', $content, $matches, PREG_OFFSET_CAPTURE); returns every CSS file that includes @font-face { ... }

这个preg_match(' /(\ @font-face)([^ }]+)(\ })/ ',内容,美元$ matches,PREG_OFFSET_CAPTURE);返回包含@font-face{…}


my function works well with CSS files that only have one match for @font-face but let say if there are 4 matches for @font-face in a single CSS file my function only adds one of them.
for example (css file content):

我的函数可以很好地处理只有一个匹配@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;
}

I want to get output like this:

我想得到这样的输出:

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

        )
)

Note that there might be @font-face with same font-family but different font-weight.

for conclusion:
How can I loop through All CSS files that contains

注意,可能存在具有相同类型但不同类型的@font-face。总结:如何循环遍历包含的所有CSS文件

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

and extract font-family and font-weight from them?

从他们身上提取字体家族和字体重量?

1 个解决方案

#1


3  

Use something like this:

使用这样的:

$fontregex = '~@font-face[^}]*?font-family:\s*"([^"]+)"[^}]*?font-weight:\s*([^;]+);[^}]*?font-style:\s*([^;]+);~';
preg_match_all($fontregex, $mycss, $matches,PREG_SET_ORDER);
print_r($matches);

See the output at the bottom of the online php demo. You'll have to fan the the result of $matches into a final array in a way that suits you.

请参见在线php演示的底部的输出。您必须以适合您的方式将$match的结果扇入最终数组。

Currently, here is the structure:

目前结构如下:

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
        )

)

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2014/07/30/3a69dbd010f104ba813f37e736dd9199.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号