[英]How to replace all urls of an @font-face src using PHP
I'm currently using a subdomain to store some files and, with that, I needed to change the links of several elements.我目前正在使用子域来存储一些文件,因此我需要更改几个元素的链接。 One of these changes includes the url of fonts.其中一项更改包括 fonts 的 url。
@font-face{
font-family:eicons;
src:url(//fonts/eicons.eot?5.10.0);
src:url(//fonts/eicons.eot?5.10.0#iefix) format("embedded-opentype"),
url(//fonts/eicons.woff2?5.10.0) format("woff2"),
url(//fonts/eicons.woff?5.10.0) format("woff"),
url(//fonts/eicons.ttf?5.10.0) format("truetype"),
url(//fonts/eicons.svg?5.10.0#eicon) format("svg");font-weight:400;font-style:normal
}
All the css referring to the fonts are stored in tags on the pages themselves, not in an external.css file.所有引用 fonts 的 css 都存储在页面本身的标签中,而不是存储在 external.css 文件中。
I'm trying to make the switch using the following regex and preg_replace我正在尝试使用以下正则表达式和 preg_replace 进行切换
$doc = new DomDocument();
$xpath = new DOMXPath($doc);
$regex_patern = "/url\((.*?)fonts/";
$doc = preg_replace($regex_patern, 'url(https://subdomain.domain.com/fonts', $doc);
echo $doc->saveHTML();
Unfortunately it ends up returning in the critical error.不幸的是,它最终返回了严重错误。
How should i fix?我应该如何解决?
I would like to do it differently, but the only way I found it was extracting the nodevalue (all text) from the tag, inserting the content into a documentfragment, changing it to the desired value and replacing the original value.我想以不同的方式做,但我发现它的唯一方法是从标签中提取节点值(所有文本),将内容插入到文档片段中,将其更改为所需的值并替换原始值。
$font_url_query = $xpath->query('//text()[contains(., "//fonts/")]');
foreach($font_url_query as $font_url) {
$regex_patern = "/url\((.*?)fonts/";
$replaced = preg_replace($regex_patern, 'url(https://subdomain.domain.com/fonts', $font_url->wholeText);
$newNodeLink = $doc->createDocumentFragment();
$newNodeLink->appendXML($replaced);
$font_url->parentNode->replaceChild($newNodeLink, $font_url);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.