从字符串返回所有匹配的CSS

问题描述 投票:0回答:1

我有一个具有以下内容的shopify CSS文件:

@font-face{font-family:"Work Sans";font-weight:600;font-style:normal;src:url("https://fonts.shopifycdn.com/work_sans/worksans_n6.136d99375282ffb6ea8c3dc4a8fe189c7be691b2.woff2?&hmac=xxxxxxxxxx") format("woff2"),url("https://fonts.shopifycdn.com/work_sans/worksans_n6.399ae4c4dd52d38e3f3214ec0cc9c61a0a67ea08.woff?&hmac=xxxxxxxxxx") format("woff")}
@font-face{font-family:"Work Sans";font-weight:400;font-style:normal;src:url("https://fonts.shopifycdn.com/work_sans/worksans_n4.29e3afeb38a0ba35e784cf169a40e8beaf814daa.woff2?&hmac=xxxxxxxxxx") format("woff2"),url("https://fonts.shopifycdn.com/work_sans/worksans_n4.e7c533c4afbed28070f6ac45dbcfe6f37840c0a8.woff?&hmac=xxxxxxxxxx") format("woff")}
@font-face{font-family:"Work Sans";font-weight:700;font-style:normal;src:url("https://fonts.shopifycdn.com/work_sans/worksans_n7.35eac55373d3da50c529c81066eb2f2f0fbedb82.woff2?&hmac=xxxxxxxxxx") format("woff2"),url("https://fonts.shopifycdn.com/work_sans/worksans_n7.1b010d40a44f517d5363112c4aff386332758bc9.woff?&hmac=xxxxxxxxxx") format("woff")}
@font-face{font-family:"Work Sans";font-weight:700;font-style:normal;src:url("https://fonts.shopifycdn.com/work_sans/worksans_n7.35eac55373d3da50c529c81066eb2f2f0fbedb82.woff2?&hmac=xxxxxxxxxx") format("woff2"),url("https://fonts.shopifycdn.com/work_sans/worksans_n7.1b010d40a44f517d5363112c4aff386332758bc9.woff?&hmac=xxxxxxxxxx") format("woff")
}.slick-slider{position:relative;display:block;box-sizing:border-box;-webkit-touch-callout:none;-webkit-user-select:none;-khtml-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;-ms-touch-action:pan-y;touch-action:pan-y;-webkit-tap-highlight-color:transparent}
....
....

我只想使用PHP从上述字符串中提取@ font-face样式。结果应该像:

@font-face{font-family:"Work Sans";font-weight:600;font-style:normal;src:url("https://fonts.shopifycdn.com/work_sans/worksans_n6.136d99375282ffb6ea8c3dc4a8fe189c7be691b2.woff2?&hmac=xxxxxxxxxx") format("woff2"),url("https://fonts.shopifycdn.com/work_sans/worksans_n6.399ae4c4dd52d38e3f3214ec0cc9c61a0a67ea08.woff?&hmac=xxxxxxxxxx") format("woff")}
@font-face{font-family:"Work Sans";font-weight:400;font-style:normal;src:url("https://fonts.shopifycdn.com/work_sans/worksans_n4.29e3afeb38a0ba35e784cf169a40e8beaf814daa.woff2?&hmac=xxxxxxxxxx") format("woff2"),url("https://fonts.shopifycdn.com/work_sans/worksans_n4.e7c533c4afbed28070f6ac45dbcfe6f37840c0a8.woff?&hmac=xxxxxxxxxx") format("woff")}
@font-face{font-family:"Work Sans";font-weight:700;font-style:normal;src:url("https://fonts.shopifycdn.com/work_sans/worksans_n7.35eac55373d3da50c529c81066eb2f2f0fbedb82.woff2?&hmac=xxxxxxxxxx") format("woff2"),url("https://fonts.shopifycdn.com/work_sans/worksans_n7.1b010d40a44f517d5363112c4aff386332758bc9.woff?&hmac=xxxxxxxxxx") format("woff")}
@font-face{font-family:"Work Sans";font-weight:700;font-style:normal;src:url("https://fonts.shopifycdn.com/work_sans/worksans_n7.35eac55373d3da50c529c81066eb2f2f0fbedb82.woff2?&hmac=xxxxxxxxxx") format("woff2"),url("https://fonts.shopifycdn.com/work_sans/worksans_n7.1b010d40a44f517d5363112c4aff386332758bc9.woff?&hmac=xxxxxxxxxx") format("woff")
}

有人可以帮助我做到这一点吗?预先感谢。

php css string extract string-matching
1个回答
0
投票

您需要为此使用正则表达式。代码:

<?php

$str = '@font-face{font-family:"Work Sans";font-weight:600;font-style:normal;src:url("https://fonts.shopifycdn.com/work_sans/worksans_n6.136d99375282ffb6ea8c3dc4a8fe189c7be691b2.woff2?&hmac=xxxxxxxxxx") format("woff2"),url("https://fonts.shopifycdn.com/work_sans/worksans_n6.399ae4c4dd52d38e3f3214ec0cc9c61a0a67ea08.woff?&hmac=xxxxxxxxxx") format("woff")}
@font-face{font-family:"Work Sans";font-weight:400;font-style:normal;src:url("https://fonts.shopifycdn.com/work_sans/worksans_n4.29e3afeb38a0ba35e784cf169a40e8beaf814daa.woff2?&hmac=xxxxxxxxxx") format("woff2"),url("https://fonts.shopifycdn.com/work_sans/worksans_n4.e7c533c4afbed28070f6ac45dbcfe6f37840c0a8.woff?&hmac=xxxxxxxxxx") format("woff")}
@font-face{font-family:"Work Sans";font-weight:700;font-style:normal;src:url("https://fonts.shopifycdn.com/work_sans/worksans_n7.35eac55373d3da50c529c81066eb2f2f0fbedb82.woff2?&hmac=xxxxxxxxxx") format("woff2"),url("https://fonts.shopifycdn.com/work_sans/worksans_n7.1b010d40a44f517d5363112c4aff386332758bc9.woff?&hmac=xxxxxxxxxx") format("woff")}
@font-face{font-family:"Work Sans";font-weight:700;font-style:normal;src:url("https://fonts.shopifycdn.com/work_sans/worksans_n7.35eac55373d3da50c529c81066eb2f2f0fbedb82.woff2?&hmac=xxxxxxxxxx") format("woff2"),url("https://fonts.shopifycdn.com/work_sans/worksans_n7.1b010d40a44f517d5363112c4aff386332758bc9.woff?&hmac=xxxxxxxxxx") format("woff")
}.slick-slider{position:relative;display:block;box-sizing:border-box;-webkit-touch-callout:none;-webkit-user-select:none;-khtml-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;-ms-touch-action:pan-y;touch-action:pan-y;-webkit-tap-highlight-color:transparent}
....
....';
preg_match_all('/@font-face\{[^\}]*\}/', $str, $matches);
var_dump($matches[0]);

结果将是:

array (size=4)
  0 => string '@font-face{font-family:"Work Sans";font-weight:600;font-style:normal;src:url("https://fonts.shopifycdn.com/work_sans/worksans_n6.136d99375282ffb6ea8c3dc4a8fe189c7be691b2.woff2?&hmac=xxxxxxxxxx") format("woff2"),url("https://fonts.shopifycdn.com/work_sans/worksans_n6.399ae4c4dd52d38e3f3214ec0cc9c61a0a67ea08.woff?&hmac=xxxxxxxxxx") format("woff")}' (length=347)
  1 => string '@font-face{font-family:"Work Sans";font-weight:400;font-style:normal;src:url("https://fonts.shopifycdn.com/work_sans/worksans_n4.29e3afeb38a0ba35e784cf169a40e8beaf814daa.woff2?&hmac=xxxxxxxxxx") format("woff2"),url("https://fonts.shopifycdn.com/work_sans/worksans_n4.e7c533c4afbed28070f6ac45dbcfe6f37840c0a8.woff?&hmac=xxxxxxxxxx") format("woff")}' (length=347)
  2 => string '@font-face{font-family:"Work Sans";font-weight:700;font-style:normal;src:url("https://fonts.shopifycdn.com/work_sans/worksans_n7.35eac55373d3da50c529c81066eb2f2f0fbedb82.woff2?&hmac=xxxxxxxxxx") format("woff2"),url("https://fonts.shopifycdn.com/work_sans/worksans_n7.1b010d40a44f517d5363112c4aff386332758bc9.woff?&hmac=xxxxxxxxxx") format("woff")}' (length=347)
  3 => string '@font-face{font-family:"Work Sans";font-weight:700;font-style:normal;src:url("https://fonts.shopifycdn.com/work_sans/worksans_n7.35eac55373d3da50c529c81066eb2f2f0fbedb82.woff2?&hmac=xxxxxxxxxx") format("woff2"),url("https://fonts.shopifycdn.com/work_sans/worksans_n7.1b010d40a44f517d5363112c4aff386332758bc9.woff?&hmac=xxxxxxxxxx") format("woff")
}' (length=348)

0
投票

使用this regular expression

/\@font\-face\{.*}/sU

这将匹配以“ @ font-face {”开头并以“}”结尾的字符串,中间包含任何字符。

在PHP中的用法:

<?php

$input = <<<EOT
@font-face{font-family:"Work Sans";font-weight:600;font-style:normal;src:url("https://fonts.shopifycdn.com/work_sans/worksans_n6.136d99375282ffb6ea8c3dc4a8fe189c7be691b2.woff2?&hmac=xxxxxxxxxx") format("woff2"),url("https://fonts.shopifycdn.com/work_sans/worksans_n6.399ae4c4dd52d38e3f3214ec0cc9c61a0a67ea08.woff?&hmac=xxxxxxxxxx") format("woff")}
@font-face{font-family:"Work Sans";font-weight:400;font-style:normal;src:url("https://fonts.shopifycdn.com/work_sans/worksans_n4.29e3afeb38a0ba35e784cf169a40e8beaf814daa.woff2?&hmac=xxxxxxxxxx") format("woff2"),url("https://fonts.shopifycdn.com/work_sans/worksans_n4.e7c533c4afbed28070f6ac45dbcfe6f37840c0a8.woff?&hmac=xxxxxxxxxx") format("woff")}
@font-face{font-family:"Work Sans";font-weight:700;font-style:normal;src:url("https://fonts.shopifycdn.com/work_sans/worksans_n7.35eac55373d3da50c529c81066eb2f2f0fbedb82.woff2?&hmac=xxxxxxxxxx") format("woff2"),url("https://fonts.shopifycdn.com/work_sans/worksans_n7.1b010d40a44f517d5363112c4aff386332758bc9.woff?&hmac=xxxxxxxxxx") format("woff")}
@font-face{font-family:"Work Sans";font-weight:700;font-style:normal;src:url("https://fonts.shopifycdn.com/work_sans/worksans_n7.35eac55373d3da50c529c81066eb2f2f0fbedb82.woff2?&hmac=xxxxxxxxxx") format("woff2"),url("https://fonts.shopifycdn.com/work_sans/worksans_n7.1b010d40a44f517d5363112c4aff386332758bc9.woff?&hmac=xxxxxxxxxx") format("woff")
}.slick-slider{position:relative;display:block;box-sizing:border-box;-webkit-touch-callout:none;-webkit-user-select:none;-khtml-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;-ms-touch-action:pan-y;touch-action:pan-y;-webkit-tap-highlight-color:transparent}
EOT;

$fonts = []; 

preg_match_all("/\@font\-face\{.*}/sU", $input, $fonts);

foreach ($fonts[0] as $font)
{
    echo ($font);
    echo ("\n");
    echo ("\n");
}
© www.soinside.com 2019 - 2024. All rights reserved.