Возможно, регулярное выражение покажется более простым решением, но оно может скрыть множество подводных камней.В этом случае я бы использовал DOM для внесения необходимых изменений.
$html = '<link href="style.css" rel="stylesheet">';
libxml_use_internal_errors(true);
$dom = new DomDocument();
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXPath($dom);
foreach ($xpath->query('//link[@rel="stylesheet"]') as $link) {
// Insert a copy of link inside the <noscript>
$noscript = $dom->createElement('noscript');
$noscript->appendChild($link->cloneNode(true));
$link->parentNode->insertBefore($noscript, $link->nextSibling);
// Modify the link attributes
$link->setAttribute('rel', 'preload');
$link->setAttribute('as', 'style');
$link->setAttribute('onload', "this.onload=null;this.rel='stylesheet'");
}
echo $dom->saveHTML();
Вышеуказанные выводы:
<link href="style.css" rel="preload" as="style" onload="this.onload=null;this.rel='stylesheet'">
<noscript><link href="style.css" rel="stylesheet"></noscript>