1 回答
TA贡献1789条经验 获得超8个赞
试试这个:
<?php
$host = 'example.com';
$stringBody = '<head>
<link rel="preload" href="/_next/list.js" as="script">
<!-- ... other link elemens -->
<style data-styled="" data-styled-version="4.2.0"></style>
</head>';
$dom = new DOMDocument();
$dom->loadHTML($stringBody);
$xpath = new DOMXPath($dom);
$headItems = $xpath->query("//head/link[@rel='preload' or @rel='stylesheet'] | //head/style");
$links = [];
foreach ($headItems as $headNode) {
if ($headNode->hasAttribute('href')) {
$headNode->setAttribute('href', $host . $headNode->getAttribute('href'));
}
$links[] = $headNode->ownerDocument->saveHTML($headNode);
}
print_r($links);
输出
Array
(
[0] => <link rel="preload" href="example.com/_next/list.js" as="script">
[1] => <style data-styled="" data-styled-version="4.2.0"></style>
)
- 1 回答
- 0 关注
- 214 浏览
添加回答
举报