php - 从字符串中返回所有匹配的 css

标签 php css string extract string-matching

我有一个包含以下内容的 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

$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)

关于php - 从字符串中返回所有匹配的 css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59160035/

相关文章:

php - PhpMyAdmin 中的登录屏幕

php - 如何使用getmxrr获取邮件服务器的主机名、ip地址?

css - Chrome CSS 宽度问题

html - 无法将样式应用于页脚 div 中的元素

javascript - 没有 JQuery 的窗口加载(和淡出)

php - 从 Controller 发送参数到 View

php - 表格可以很好地在 html 中显示,但不能在使用 mpdf 生成的 pdf 中显示

linux - 在 BASH 中通过 Char 循环替换 Char

c++ - 如何在C++中压缩一个字符串?

PHP删除字符串中的重复模式